请帮帮我。我有桌子:
id|count
1 |1
1 |3
2 |1
2 |2
3 |1
3 |2
我需要为同一个count
选择id
(列)1和2的ID:
id
2
3
答案 0 :(得分:3)
您可以使用group by
和having
:
select id
from t
where count in (1, 2)
group by id
having count(*) = 2;
答案 1 :(得分:1)
你可以试试这个
select t1.id
from myTable t1
join myTable t2
on t1.id = t2.id
where t1.count = 1 and t2.count = 2
答案 2 :(得分:0)
使用INTERSECT
查找包含1和2的ID:
SELECT id FROM table WHERE count = 1
INTERSECT
SELECT id FROM table WHERE count = 2
编辑:哎呀,MySQL是否支持INTERSECT
?
答案 3 :(得分:0)
使用GROUP BY子句:
SELECT DISTINCT id FROM Your_Table
WHERE [count] IN (1, 2)
GROUP BY id
HAVING COUNT(*) > 2
答案 4 :(得分:0)
Select id
FROM TABLE_NAME
WHERE count = 1
INTERSECT
Select id
FROM TABLE_NAME
WHERE count = 2;
那应该有用。以下是使用交叉https://www.tutorialspoint.com/sql/sql-intersect-clause.htm
的链接