简体   繁体   English

MySQL跨多个列搜索重复项

[英]MySQL Search for Duplicates across multiple columns

APOLOGIES : I meant to say that the duplicates have to be within the same column. 道歉 :我的意思是说重复项必须在同一列中。

I have the following code: 我有以下代码:

SELECT m.* FROM ( 
    SELECT text 
    FROM mytable 
    GROUP BY text 
    HAVING COUNT(*) > 1 ) q 
JOIN mytable m 
ON m.text = q.text

This enables me to search for duplicates on the column text and works fine. 这使我可以在列text上搜索重复项,并且工作正常。 However, I have a table with: 但是,我有一张桌子,上面有:

q1a, q2a, q3a, q4a, q5a

What I'd like is for the the query to check in all the columns for duplicates - if this possible and could anyone give me a pointed? 我要查询的是在所有列中检查重复项-如果可能的话,有人可以给我指出吗?

Thanks, 谢谢,

H. H。

This may be what you need, it will give you the text that is duplicated, and on what rows and column the match is found; 这可能是您所需要的,它将为您提供重复的文本以及找到匹配项的行和列;

SELECT GROUP_CONCAT(id) ids,q1a text,1 col 
FROM myTable GROUP BY text HAVING COUNT(*)>1
UNION
SELECT GROUP_CONCAT(id),q2a text,2 
FROM myTable GROUP BY text HAVING COUNT(*)>1
UNION
SELECT GROUP_CONCAT(id),q3a text,3  
FROM myTable GROUP BY text HAVING COUNT(*)>1
UNION
SELECT GROUP_CONCAT(id),q4a text,4  
FROM myTable GROUP BY text HAVING COUNT(*)>1
UNION
SELECT GROUP_CONCAT(id),q5a text,5 
FROM myTable GROUP BY text HAVING COUNT(*)>1

An SQLfiddle to test with . 要使用进行测试的SQLfiddle

If I undestand your question correctly, you could use a query like this: 如果我正确理解您的问题,则可以使用以下查询:

SELECT m.*
FROM (
    SELECT txt FROM (
      SELECT q1a as txt FROM mytable
      UNION ALL
      SELECT q2a as txt FROM mytable
      UNION ALL
      SELECT q3a as txt FROM mytable
      UNION ALL
      SELECT q4a as txt FROM mytable
      UNION ALL
      SELECT q5a as txt FROM mytable) u
    GROUP BY
      txt
    HAVING COUNT(*)>1) q
  INNER JOIN mytable m
  ON q.txt IN (m.q1a, m.q2a, m.q3a, m.q4a, m.q5a)

Please see fiddle here . 请看这里的小提琴。

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

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