如何在两个镜像列中查找mysql重复项

时间:2016-03-14 20:41:51

标签: mysql duplicates mirror

在MySQL数据库中我有行。例如:

Varchar1    | Varchar2    
XXXXXXXXXX   YYYYYYYYYY   
YYYYYYYYYY   XXXXXXXXXX 
XXXXXXXXXX   CCCCCCCCCC
YYYYYYYYYY   AAAAAAAAAA
CCCCCCCCCC   AAAAAAAAAA   

如何在第1行和第2行中找到(并删除)duplikates?

不应删除一行(1或2)。

2 个答案:

答案 0 :(得分:1)

SELECT t1.id, t1.varchar1,t1.varchar2,t2.id, t2.varchar1,t2.varchar2
from yourtable t1, yourtable t2
where t1.id<>t2.id and t1.varchar1=t2.varchar2

试试这个,如果不起作用,请回来:)

如果这会检索您想要的数据,那么删除非常简单:

DELETE from yourtable WHERE id in( SELECT t1.id from yourtable t1, yourtable t2
    where t1.id<>t2.id and t1.varchar1=t2.varchar2)

警告:仅当您不在生产环境中或者您确定自己在做什么或是否在交易中时才使用DELETE(BEGIN ...)

答案 1 :(得分:0)

解决。 我已经使用varchar1和varchar2的散列的按位AND添加BIGINT值。

ALTER TABLE t1 ADD COLUMN hex1 BIGINT UNSIGNED NOT NULL; ALTER TABLE t1 ADD COLUMN hex2 BIGINT UNSIGNED NOT NULL; ALTER TABLE t1 ADD COLUMN hex BIGINT UNSIGNED NOT NULL; UPDATE t1 SET hex1 = CONV(SUBSTRING(CAST(SHA(varchar1) AS CHAR), 1, 16), 16, 10), hex2 = CONV(SUBSTRING(CAST(SHA(varchar2) AS CHAR), 1, 16), 16, 10), hex = hex1&hex2; ALTER TABLE t1 DROP COLUMN hex1; ALTER TABLE t1 DROP COLUMN hex2;

并按十六进制列分组

SELECT t1.* GROUP BY hex;