MySQL在多列中搜索重复项

时间:2013-06-22 09:12:42

标签: mysql duplicates

APOLOGIES :我想说重复项必须在同一列中。

我有以下代码:

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

这使我能够在列text上搜索重复项并且工作正常。但是,我有一张表:

q1a, q2a, q3a, q4a, q5a

我想要的是查询检查重复项的所有列 - 如果这可能并且任何人都可以给我一个指向?

谢谢,

小时。

2 个答案:

答案 0 :(得分:1)

这可能是您需要的,它会为您提供重复的文本,以及找到匹配的行和列;

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

答案 1 :(得分:0)

如果我理解你的问题,你可以使用这样的查询:

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)

请参阅小提琴here