首页 > 解决方案 > 如何在 mySQL DB 中查找没有重复的行?

问题描述

我有一张桌子:

--id---name---col1--col2--col3...-colN--created. 
--1---myName---col1--col2--col3...-colN--created1. 
--2---myName---col1--col2--col3...-colN--created2. 
--3---myOtherName---Othercol1--Othercol2--Othercol3...-OthercolN--created3. 

id并且created字段是唯一的。

其余行有重复 - 完全相同的一组值 (name+col1+col2+col3+..+colN)。

但是,很少有行是完全唯一的。我怎么能找到它们row 3在我的例子中)?

标签: mysqlsql

解决方案


您可以使用NOT EXISTS相关子查询从同一个表中选择具有不同 ID 但值相等的行。

SELECT *
       FROM elbat t1
       WHERE NOT EXISTS (SELECT *
                                FROM elbat t2
                                WHERE t2.id <> t1.id
                                      AND t2.col1 = t1.col1
                                      AND t2.col2 = t1.col2
                                      AND t2.col3 = t1.col3
                                      ...
                                      AND t2.coln = t1.coln);

推荐阅读