我有一个红移表(user_group
)
user_id | group_id
--------+---------
1 | 1
1 | 2
2 | 1
2 | 3
3 | 2
3 | 3
4 | 4
编写查询以返回属于组(user_id
的任意子集的所有用户(group_id
)的最佳方法是什么?
现在,我被黑的查询如下,我只在需要时添加/删除WHERE
过滤器:
SELECT DISTINCT user_id
FROM
user_group
WHERE
user_id IN (SELECT DISTINCT user_id FROM user_group WHERE group_id = 1)
AND user_id IN (SELECT DISTINCT user_id FROM user_group WHERE group_id = 5)
...
而且,感觉应该有更好的方法吗?
答案 0 :(得分:3)
使用GROUP BY
和HAVING
:
SELECT user_id
FROM user_group
WHERE group_id IN (1, 5)
GROUP BY user_id
HAVING COUNT(*) = 2; -- number of groups in the `IN` list
答案 1 :(得分:1)
属于 ANY 所列组的用户:
SELECT DISTINCT
user_id
FROM user_group
WHERE group_id in (1, 5)
属于 ALL 个列出的组的用户:
SELECT
user_id
FROM user_group
GROUP BY user_id
HAVING SUM(CASE WHEN group_id = 1 THEN 1 END) > 0
AND SUM(CASE WHEN group_id = 2 THEN 1 END) > 0