简体   繁体   English

MySQL在多列中找到重复项

[英]MySQL find duplicates in multiple columns

I have a table with user IDs split into 2 columns. 我有一个用户ID分为2列的表。 (To explain this a little more, we capture the IDs of participants by scanning barcodes. Sometimes the barcode scanner function doesn't work for whatever reason, so we also allow manual entry of the ID, IF the barcode scanner doesn't work.) This results in data like the following: (为进一步说明这一点,我们通过扫描条形码来捕获参与者的ID。有时条形码扫描器功能由于某种原因无法使用,因此,如果条形码扫描器不起作用,我们也允许手动输入ID。 )这将产生如下数据:

+------+-----------+
|  ID  | ID_MANUAL |
+------+-----------+
| A    | NULL      |  
| NULL | A         |  
| B    | NULL      |  
| B    | NULL      |  
| NULL | C         |  
| C    | NULL      |  
| NULL | D         |  
| NULL | D         |  
+------+-----------+

I want to find all of the duplicate IDs, taking both columns into account. 我想找到所有重复的ID,同时考虑到这两列。 It's easy to find the duplicates that are only in 1 column ("B" and "D"). 很容易找到仅在1列(“ B”和“ D”)中的重复项。 But how do I find the duplicates "A" and "C"? 但是,如何找到重复的“ A”和“ C”呢? Ideally, the query would find and return ALL duplicates (A,B,C, and D). 理想情况下,查询将查找并返回所有重复项(A,B,C和D)。

Thanks! 谢谢!

Try this: 尝试这个:

SELECT DUP.* FROM (SELECT ID FROM yourtable) ORI
LEFT JOIN yourtable DUP ON DUP.ID = ORI.ID_MANUAL WHERE DUP.ID IS NOT NULL 

An advice: a field named ID m,ust be unique and not null. 一个建议:一个名为ID m的字段必须是唯一的,不能为null。 But if you have this structure, you can try this: 但是,如果您具有这种结构,则可以尝试以下操作:

SELECT id
FROM yourtable t
WHERE id is not null
AND
    (SELECT COUNT(*)
    FROM yourtable t2
    WHERE t2.id = t.id) +
    (SELECT COUNT(*)
    FROM yourtable t3
    WHERE t3.id_manual = t.id) > 1

UNION

SELECT id_manual
FROM yourtable t
WHERE id_manual is not null
AND
    (SELECT COUNT(*)
    FROM yourtable t2
    WHERE t2.id = t.id_manual) +
    (SELECT COUNT(*)
    FROM yourtable t3
    WHERE t3.id_manual = t.id_manual) > 1

You can go on Sql Fiddle 您可以继续Sql Fiddle

You could try UNION ALL here: 您可以在这里尝试UNION ALL

select id,count(*)
from
(
 select id
 from yourtable
 union all
 select id_manual as id
 from yourtable
) a
group by id
having count(*) >1;

try: 尝试:

select id, count(*)
from
(
 select id
 from data
 where id_manual is null
 union all
 select id_manual as id
 from data
 where id is null
) a
group by id
having count(*) > 1;

and

select id, id_manual
from data
group by id, id_manual
having count(*) > 1;

You can do this with a simple JOIN, using COALESCE and DISTINCT if you have a surrogate auto-increment primary key: 如果您有代理自动增量主键,则可以使用COALESCE和DISTINCT进行简单的JOIN操作:

SELECT DISTINCT s2.pk, s2.ID, s2.ID_MANUAL
FROM scans s1
JOIN scans s2
ON COALESCE(s2.ID, s2.ID_MANUAL) = COALESCE(s1.ID, s1.ID_MANUAL)
AND s2.pk > s1.pk

This will exclude the original record, so you could delete the records returned in this result set. 这将排除原始记录,因此您可以删除此结果集中返回的记录。

Here's the SQL Fiddle . 这是SQL Fiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM