SQL 查找违反 UNIQUE together 索引的行 [英] SQL Find rows that violate UNIQUE together index

查看:15
本文介绍了SQL 查找违反 UNIQUE together 索引的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

您可以使用 Group ByHaving 来解决这个问题:

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.

这篇关于SQL 查找违反 UNIQUE together 索引的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆