我正在尝试进行mysql搜索,该搜索返回带有分组类别(group_id)的用户信息。我只是需要一些帮助才能正确查询我的信息:
SELECT a.*, a.name AS editor, a.name AS created_by, b.*, c.* FROM `cm0x8_users` AS a LEFT JOIN cm0x8_users_xref AS b ON b.userid = a.id LEFT JOIN cm0x8_user_usergroup_map AS c ON c.user_id = a.id WHERE (a.block IN (0, 1)) AND c.group_id NOT IN(6,7,8) ORDER BY a.name asc
返回同一个用户3次(他们分为3个不同的组)。
示例:
id name group_id
1 Fred 1
1 Fred 2
1 Fred 3
2 John 1
3 Jane 1
3 Jane 3
但我在追求:
id name group_id
1 Fred 1, 2, 3
2 John 1
3 Jane 1, 3
(或类似)
任何帮助都会很棒!
答案 0 :(得分:1)
这似乎给了我预期的结果..
SELECT a.*,a.name AS editor,a.name AS created_by,b.*,group_concat(c.group_id) as gid,c.* FROM `#__users` AS a LEFT JOIN #__users_xref AS b ON b.userid = a.id LEFT JOIN #__user_usergroup_map AS c ON c.user_id = a.id WHERE (a.block IN (0, 1)) AND c.group_id NOT IN(6,7,8) GROUP BY a.id ORDER BY a.name asc