我有桌子:
id filter_id
1 5
2 5
3 5
3 17
4 17
5 17
ID不是唯一的
我需要像
这样的东西SELECT id FROM table WHERE filter = 5 AND filter = 17
但它没有返回任何东西,因为过滤列不能同时为5和17
所以我需要得到id = 3的结果。
我试过
SELECT id FROM table WHERE filter IN (5, 17)
但我需要AND,而不是OR操作
感谢您的帮助。
答案 0 :(得分:3)
您可以使用:
SELECT id
FROM table
WHERE filter IN (5,17)
GROUP BY id
HAVING COUNT(DISTINCT filter) = 2
答案 1 :(得分:2)
select id from table
where filter_id IN (5, 17)
group by id
having count(distinct filter_id) =2
答案 2 :(得分:2)
答案 3 :(得分:0)
如果表格中有这么多行,则不对数据进行分组:
SELECT distinct id FROM table t1 WHERE filter_id = 5 and exists (select 1 from table t2 where filter_id = 17 and t1.id = t2.id);