日期:2014-05-18  浏览次数:20560 次

求助一个查询和删重复记录的方法
此表共有3列  A   (主列) B     C  

列  A                B                   C  
                1               good                     bad  
                2               good                     bad  
                3               good                     bad  

由于我在更新其它关联表的时候,造成 B   C列有很多重复的数据(如上列子),我怎么查出来(只查重复的),并删除后只保留一条记录。

谢谢



------解决方案--------------------
查询:
select * from 表 a
where exists (
select 1 from 表
where b=a.b and c=a.c
and a <> a.a
)

删除:
delete a
from 表 a
where exists (
select 1 from 表
where b=a.b and c=a.c
and a <a.a
)

------解决方案--------------------
select * from 表 as T
where id > (select min(id) from 表 where A=T.A)

------解决方案--------------------
delete from table_Pqs where exists(
select 1 from table_Pqs as tmp where tmp.b=table_Pqs.b and tmp.c=table_Pqs.c and
tmp.a <table_Pqs.a
)
------解决方案--------------------
(
select 1 from 表
where b=a.b and c=a.c
and a <> a.a
) 这是什么意思,楼下的帮我描述一下吧。

意识就是用这个表和上面的别名为a的表关联,查数内容一样但是A列不一样的数据来然后删除
改成下面的样子比较容易理解

delete a
from 表 别名1
where exists (
select 1 from 表 别名2
where 别名1.b=别名2.b and 别名1.c=别名2.c
and 别名1.a <别名2.a
)