【问题标题】:MySQL return only users in a specified groupMySQL 仅返回指定组中的用户
【发布时间】:2016-03-28 05:07:18
【问题描述】:

在 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),我想查找特定组中的所有其他用户,我的登录用户也是其中的成员。如何获取该组中的用户列表?

我需要使用表单提供的文本来查找组名,用户 ID 将从身份验证/登录代码中获取。如果用户不属于组,他们应该无法获得组成员的列表。

对于红色组,当我以 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

对于蓝色组,如果我以 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

对于绿色组,当我以 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

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

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

【问题讨论】:

    标签: mysql pivot-table group-concat


    【解决方案1】:

    这是一个带有子查询的示例,显示您的问题中显示的结果。

    乔展示蓝色组:

    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

    【讨论】:

    • 这很奏效,感谢您的提琴。还有一个请求——如果匹配失败,我将如何不返回任何行——目前总是有一行返回正确的用户列表或 NULL。如果Users返回NULL,我想返回一个空集。
    • sqlfiddle.com/#!9/80da98 这个小提琴对我的实际表设计更具体一些,我还想检查每个用户和每个组的启用标志,并且只返回启用组中启用用户的结果。除了用户名,我还想在Users 结果列中返回电子邮件地址。如果我自己解决,我会发布答案。
    • 这里是小提琴:sqlfiddle.com/#!9/80da98/8/0。刚刚添加了启用标志并将用户/组条件移动到主查询,因此当它不匹配任何组时不会返回任何行。
    • 最后一件事,如何将每个用户连续返回用户而不是连续返回一行?请参阅原始问题的更新 #2。
    猜你喜欢
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    相关资源
    最近更新 更多