从Mysql表的3列中搜索重复值

问题描述 投票:-2回答:2

表A具有5个列ID,(主列)c1,c2,c3,c4

表A中的值不应为两倍。假设c1中的值100123。

此值不应在任何其他列中。

如果任何值都位于两列中,则在其所在的第二行中打印。

表A具有一百万行。

如何通过存储过程或函数或mysql中的SQL解决?

mysql stored-procedures stored-functions
2个回答
0
投票
SELECT c.id
     , c.col1
     , c.col2
     , c.col3
     , c.col4
     , d.id    AS d_id
     , d.col1  AS d_col1
     , d.col2  AS d_col2
     , d.col3  AS d_col3
     , d.col4  AS d_col4
  FROM mytable c
  JOIN mytable d
    ON c.id < d.id
   AND ( d.col1 IN (c.col1,c.col2,c.col3,c.col4)
      OR d.col2 IN (c.col1,c.col2,c.col3,c.col4)  
      OR d.col3 IN (c.col1,c.col2,c.col3,c.col4)  
      OR d.col4 IN (c.col1,c.col2,c.col3,c.col4)
       )

0
投票

这不需要存储过程或存储函数。

这是我的解决方法:

SELECT c1, GROUP_CONCAT(id), COUNT(*)
FROM (
    SELECT id, c1 FROM tableA
    UNION ALL
    SELECT id, c2 FROM tableA
    UNION ALL
    SELECT id, c3 FROM tableA
    UNION ALL
    SELECT id, c4 FROM tableA
) AS t
GROUP BY c1
HAVING COUNT(*) > 1
© www.soinside.com 2019 - 2024. All rights reserved.