0

道歉:我的意思是说重复的必须在同一列中。

我有以下代码:

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

我想要的是查询检查所有列是否有重复项 - 如果可能的话,有人可以给我指出吗?

谢谢,

H。

4

2 回答 2

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

一个用于测试的 SQLfiddle

于 2013-06-22T09:34:32.770 回答
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)

在此处查看小提琴。

于 2013-06-22T09:27:24.023 回答