与MySQL Markup Records with Duplicates on Select连接。不同之处在于,我希望不仅标记实际重复的记录,而且所有记录具有相同的值,包括第一个。
id name
--------------
1 John # mark this
2 Peter
3 John # this
4 David
5 John # and this
@ m-khalid-junaid https://stackoverflow.com/a/47728321/1056384提出的方法
SELECT DISTINCT a.*,
CASE WHEN b.id IS NULL THEN 0 ELSE 1 end `duplicate`
FROM tab a
LEFT JOIN tab b ON a.name = b.name AND a.id > b.id
ORDER BY a.id
对我来说有一两个问题:
order by
id (不确定是否依赖)但我需要通过不同的方式对真实复杂查询中的结果进行排序。如果查询优化有意义,我的任务有点复杂。实际上,我只需要在组内标记重复项:
id group name
--------------
1 1 John # mark this (dups in group #1)
2 1 Peter
3 1 John # mark this (dups in group #1)
4 2 David
5 2 John # this is not (it's in group #2)
答案 0 :(得分:1)
看起来这对我有用:
SELECT DISTINCT t.*,
IF(t2.id IS NULL, 0, 1) AS is_duplicate
FROM tab t
LEFT JOIN tab t2 ON t.name = t2.name AND t.group = t2.group AND t.id <> t2.id
答案 1 :(得分:0)
虽然你可以根据需要调整给定的查询,但我会在这里使用EXISTS子查询而不是连接。
select t.id, t.group, t.name, exists (
select * from tab t2
where t2.group = t.group
and t2.name = t.name
and t2.id <> t.id
) as is_duplicate
from tab t
order by t.group, is_duplicate, t.name