MySQL仅返回指定组

时间:2015-12-22 01:51:23

标签: mysql pivot-table group-concat

在MySql数据库中,我有一个相当普通的用户和组,其中包含一个数据库users_groups,以允许N:M关系。

users

id      | name
--------+----------
1       | Joe
2       | Anna
3       | Max

groups

id       | name
---------+----------
1        | Red
2        | Blue
3        | Green

users_groups

id       | userid | groupid
---------+--------+---------
1        | 1      | 2
2        | 3      | 2
3        | 1      | 3 
3        | 2      | 1

所以...... Red(1)组的成员是Anna(2),Green(3)组的成员是Joe(1),Blue(2)组的成员是Joe(1)和Max (3)。

当用户登录时,我有用户ID(例如Joe为1),我想查找我的登录用户也是其成员的特定组中的所有其他用户。如何获取该组中的用户列表?

我需要使用表单提供的文本查找组名,并且将从auth / login代码中获取userid。如果用户不属于该组,则他们无法获得组成员列表。

对于Red组,我只能在以Anna身份登录时看到一个用户(Anna)

User  | Group | Users in Group must include the current user
------+------------
1     | Red   | EMPTY

User  | Group | Users in Group must include the current user
------+------------
2     | Red   | Anna

User  | Group | Users in Group must include the current user
------+------------
3     | Red   | EMPTY

对于Blue group,如果我以Joe或Max身份登录,那么我应该会看到一个用户列表(Joe和Max)

User  | Group | Users in Group must include the current user
------+------------
1     | Blue  | Joe, Max

User  | Group | Users in Group must include the current user
------+------------
2     | Blue  | EMPTY

User  | Group | Users in Group must include the current user
------+------------
3     | Blue  | Joe, Max

对于Green组,我只能在以Joe

登录时看到一个用户(Joe)
User  | Group | Users in Group must include the current user
------+------------
1     | Green   | Joe

User  | Group | Users in Group must include the current user
------+------------
2     | Green   | EMPTY

User  | Group | Users in Group must include the current user
------+------------
3     | Green   | EMPTY

===更新#1 ===

使用@ Erico的答案和下面的小提琴以及更新的表架构来包含已启用和电子邮件字段,我可以使用其他enabled列检查执行以下操作。但是,我想将所有用户作为单独的行返回到结果集中,而不是单个Users列,其中包含所有数据。

http://sqlfiddle.com/#!9/80da98/2

SELECT '1' as User, name as Group,
(SELECT GROUP_CONCAT(email) FROM users u, users_groups ug 
 WHERE u.enabled = 1 AND u.id = ug.user_id AND ug.group_id = g.id AND ug.group_id 
 IN (SELECT group_id FROM users_groups WHERE user_id = 1)
) as Users
FROM groups g
 WHERE g.name = 'Blue' AND g.enabled = 1

===更新#2 ===

而不是将结果返回到一行:

User  | Group | Users in Group must include the current user
------+------------
1     | Blue  | Joe, Max

或使用电子邮件而非名称

User  | Group | Users in Group must include the current user
------+------------
1     | Blue  | joe@mycompany.com, max@hiscompany.com

我想每个用户连续返回用户的完整信息,因此搜索组Blue(2)中的用户Joe(1)将返回:

User  | Name  | Email
------+------------
1     | Joe   | joe@mycompany.com
3     | Max   | max@hiscompany.com

1 个答案:

答案 0 :(得分:1)

以下是一个子查询示例,显示问题中显示的结果。

Joe showinng group Blue:

SELECT '1' as `User`, name as `Group`, 
(SELECT GROUP_CONCAT(name) FROM users u, users_groups ug 
 WHERE u.id = ug.user_id AND ug.group_id = g.id 
 AND ug.group_id IN (SELECT group_id FROM users_groups WHERE user_id = 1)
 ) as `Users` 
FROM groups g
 WHERE g.name = 'Blue'

http://sqlfiddle.com/#!9/1a735/22/0