考虑下面的表'Table1'
main_id main_item_id
-------- ---------
1 101
1 102
2 105
2 105
3 105
3 106
4 101
4 101
4 102
我需要获取main_id 2和4,因为它在100万条其他记录中有重复的main_item_id
提前致谢。
答案 0 :(得分:1)
使用group by
子句检查重复
SELECT main_id, main_item_id
FROM table
GROUP BY main_id, main_item_id
HAVING count(*) > 1
答案 1 :(得分:1)
这将选择所有具有2个或更多相同main_item_id的唯一main_id:
SELECT DISTINCT T.main_id
FROM YourTable T
GROUP BY T.main_id
, T.Main_item_id
HAVING COUNT(1) > 1