使用count和distinct(去重)关键字可以查看数据库某列是否有重复项。例如:
selectcount(discinct(colunmname))fromtable_name
如果上述查询统计结果大于count(colunmname),则代表这一列有重复项。
扩展资料
SQLSELECTDISTINCT语句用法介绍:
在表中,可能会包含重复值。这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值。
关键词DISTINCT用于返回唯一不同的值。
语法:
SELECTDISTINCT列名称FROM表名称
使用DISTINCT关键词,例如要从"Company"列中选取所有的值,我们需要使用SELECT语句:
SELECTCompanyFROMOrders
distinct。SQLserver中很明显的去重复的语句是distinct。selectdistinct是去除重复的记录行,count(distinctColumn),消除重复值。还有一些不明显的具有去重功能的词,例如union,会去除重复的记录行或值。
sql查询去除重复值语句\x0d\x0asql 单表/多表查询去除重复记录\x0d\x0a单表distinct\x0d\x0a\x0d\x0a多表group by\x0d\x0a\x0d\x0agroup by 必须放在 order by 和 limit之前,不然会报错\x0d\x0a\x0d\x0a************************************************************************************\x0d\x0a\x0d\x0a1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断\x0d\x0a\x0d\x0aselect * from people\x0d\x0a\x0d\x0awhere peopleId in (select peopleId from people group by peopleId having count(peopleId) >1)\x0d\x0a2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录\x0d\x0a\x0d\x0adelete from people\x0d\x0awhere peopleId in (select peopleId from people group by peopleId having count(peopleId) >1)\x0d\x0aand rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)\x0d\x0a3、查找表中多余的重复记录(多个字段)\x0d\x0a\x0d\x0aselect * from vitae a\x0d\x0awhere (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) >1)\x0d\x0a4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录\x0d\x0adelete from vitae a\x0d\x0awhere (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) >1)\x0d\x0aand rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)\x0d\x0a5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录\x0d\x0a\x0d\x0aselect * from vitae a\x0d\x0awhere (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) >1)\x0d\x0aand rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>欢迎分享,转载请注明来源:内存溢出
评论列表(0条)