delete from 表 where id not in(select min(id) from 表 group by 公司名称 having count(*)>0)
果然不能以星评人,高手! 还请 详细指点一下这句放的意思 关键是 having count(*) >0 是什么意思呢 ?
可以用臨時表處理select distinct * into tab from tb delete from tb insert into tb select * from tab drop table tab
having count(*)>0 为了选择其他没有包括在group by 中的字段,得以显示,在这可以不用!
delete from 表 where id not in(select min(id) from 表 group by 公司名称 having count(*)>0)--此条语句即是删除相同的记录,保留最小ID的一行记录。首先选出同公司名称的最小ID,再删除所选以外的所有记录。having count(*)>0 是让记录有至少有一行满足以上条件防止ID为空
前面写错了,exists应该这样写("公司名称"重复的纪录留下id最小的那一个): delete from 表 where exists(select * from 表 a where a.公司名称=表.公司名称 and a.id<表.id)用exists比用in效率高
同意风云的说法,exists的效率比较高,同时你的大小写也会影响效率的!
delete a from 表 a where exists(select 1 from 表 where 公司名称=a.公司名称 and id<a.id)
delete from 表 where exists(select * from 表 a where a.公司名称=表.公司名称 and a.id<表.id)用exists比用in效率高
where id not in(select min(id) from 表 group by 公司名称 having count(*)>0)
服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: 'a' 附近有语法错误。
to :
回复人: xluzhong(打麻将一缺三,咋办?) ( ) 信誉:100 2005-01-10 19:14:00 得分: 0
delete from 表
where id not in(select min(id) from 表 group by 公司名称 having count(*)>0)
果然不能以星评人,高手!
还请 详细指点一下这句放的意思
关键是 having count(*) >0 是什么意思呢 ?
delete from tb
insert into tb select * from tab
drop table tab
where id not in(select min(id) from 表 group by 公司名称 having count(*)>0)--此条语句即是删除相同的记录,保留最小ID的一行记录。首先选出同公司名称的最小ID,再删除所选以外的所有记录。having count(*)>0 是让记录有至少有一行满足以上条件防止ID为空
delete from 表
where exists(select * from 表 a where a.公司名称=表.公司名称 and a.id<表.id)用exists比用in效率高
where exists(select * from 表 a where a.公司名称=表.公司名称 and a.id<表.id)用exists比用in效率高