数据库中有个大表,需要查找其中的名字有重复的记录id,以便比较。如果仅仅是查找数据库中name不重复的字段,很容易:
SELECT min(`id`),`name` FROM `table` GROUP BY `name`
但是这样并不能得到说有重复字段的id值。(只得到了最小的一个id值)查询哪些字段是重复的也容易:
SELECT `name`,count(`name`) as count FROM `table` GROUP BY `name` HAVING count(`name`) >1 ORDER BY count DESC
但是要一次查询到重复字段的id值,就必须使用子查询了,于是使用下面的语句。
SELECT `id`,`name` FROM `table` WHERE `name` in (
SELECT `name`
FROM `table`
GROUP BY `name` HAVING count(`name`) >1)
但是这条语句在mysql中效率太差,感觉mysql并没有为子查询生成零时表。于是使用先建立零时表:
create table `tmptable` as (
SELECT `name`
FROM `table`
GROUP BY `name` HAVING count(`name`) >1)
然后使用多表连接查询:
SELECT a.`id`, a.`name` FROM `table` a, `tmptable` t WHERE a.`name` = t.`name`
结果这次结果很快就出来了。
========================
查询及删除重复记录的方法
(一)
1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select * from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) >1)
2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
delete from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) >1)
and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)
3、查找表中多余的重复记录(多个字段)
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) >1)
4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) >1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) >1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
oracle数据库下如果记录数不多可以使用下面的查询
delete
from
TAB_NAME
a
where
a.rowid
<>
(select
max(rowid)
from
TAB_NAME
b
where
b.COL1
=
a.COL1)
括号中的where后,你可以加入所有字段相等
b.COL2
=
a.COL2
and
b.COL3
=
a.COL3
……
sqlserver下
select
distinct
*
from
tableName
就可以得到无重复记录的结果集。
如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除
select
distinct
*
into
#Tmp
from
tableName
drop
table
tableName
select
*
into
tableName
from
#Tmp
drop
table
#Tmp
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)