9

我想在表中的两个(或更多)列上放置唯一索引,但我得到“找到重复键”。如何选择那些导致重复的行?

4

2 回答 2

14

您可以为此使用Group By和:Having

SELECT col1,
       col2
FROM   table
GROUP  BY col1,
          col2
HAVING Count(*) > 1

基本上 - 对值进行分组,然后过滤多个实例。

于 2012-08-20T07:41:57.860 回答
0

您可以使用以下方法之一:

SELECT t1.rowid
FROM   this_table t1
WHERE  EXISTS (SELECT '1'
               FROM   this_table t2
               WHERE  t2.column_value1 = t1.column_value1
                      AND t2.column_value2 = t1.column_value2
                      AND t2.rowid > t1.rowid);

SELECT *
FROM   this_table_name a
WHERE  a.rowid > ANY (SELECT b.rowid
                      FROM   this_table_name b
                      WHERE  a.col1 = b.col1
                             AND a.col2 = b.col2);

SELECT my_column,
       Count(my_column)
FROM   this_table_name
GROUP  BY my_column
HAVING Count (my_column) > 1;
于 2012-08-20T07:55:44.870 回答