说我有一张桌子
id sequence card_tap time
1 1 61 1
1 2 62 10
1 3 2 20
2 11 12 5
2 12 61 10
2 12 62 20
我想在每个sequence
组中找到重复的id
(ID存在),在这种情况下
id sequence card_tap time
2 12 61 10
2 12 62 20
我可以问一下如何做到这一点,最好快点,因为我有一个大型数据集。
答案 0 :(得分:0)
Dunno这个能够多快,如果不在id / sequence上聚合,就无法找到任何方法。
SELECT id,sequence,count(*)as n_rows
FROM a
GROUP BY id,序列
拥有n_rows> 1
答案 1 :(得分:0)
这些是我的第一直觉:
SELECT *
FROM the_table
WHERE (id, sequence) IN (
SELECT id, sequence
FROM the_table
GROUP BY id, sequence
HAVING COUNT (*) > 1
);
或
SELECT *
FROM the_table AS t
INNER JOIN (
SELECT id, sequence
FROM the_table
GROUP BY id, sequence
HAVING COUNT (*) > 1
) AS s USING (id, sequence)
;
就个人而言,我首先使用JOIN,根据我的经验,他们可以更好地利用索引......如果你有一个(id,序列)。
答案 2 :(得分:0)
使用in
:
select t.*
from t
where (t.id, t.sequence) in (select t2.id, t2.sequence
from t t2
group by t2.id, t2.sequence
having count(*) > 1
);