简化MySQL查询?

时间:2014-11-27 13:40:06

标签: mysql

我在MySQL DB中有三个表如下

  

`users(id_user - 99911,username - heman,status - manager)

     

service(id_service - 11,service_name - blabla)

     

auth_service(id_user - 99911,id_service - 11)

所有这三个表都不是关系型的。我想在PHP代码中完成它。我尝试过一些查询来显示授权给只有经理的用户的服务。

select s.service_name from service s 
  inner join auth_service a on s.id_service = a.id_service 
  inner join users u on a.id_user = 99911 
order by s.service_name and u.status='manager';

但它显示多次相同的记录已被排序。问题出在哪儿?如何避免重复显示相同的记录?

2 个答案:

答案 0 :(得分:0)

以下查询为您提供经理用户授权的所有服务。

select distinct s.service_name, a.id_user
from service s inner join auth_service a 
on s.id_service = a.id_service 
where a.id_user in (select id_user from users where status = 'manager') 
order by s.service_name

答案 1 :(得分:-1)

使用GROUP BY子句。

select DISTINCT s.service_name from service s 
  inner join auth_service a on s.id_service = a.id_service 
  inner join users u on a.id_user = 99911 
order by s.service_name and u.status='manager'