SQL Find rows that violate UNIQUE together index

后端 未结 2 472
伪装坚强ぢ
伪装坚强ぢ 2021-01-11 23:44

i want to put unique index on two (or more) columns in a table, but i get \"duplicate keys found\". How to select those rows which cause duplication?

2条回答
  •  感情败类
    2021-01-12 00:05

    You can use Group By and Having for this:

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

    Basically - group the values, then filter for instances where there is more than one.

提交回复
热议问题