分享、学习、提高
2008/11/05 13:36
文章作者:Enjoy 转载请注明原文链接。
稍微解释下:
先把要唯一的字段(我这里是licenseID)用group把多于1个的数据找出来做为b表,同时把最小的ID选中,然后于原表a表关联,把大于最小ID的数据删除。

delete hxtable as a from hxtable a,
(
select licenseID,min(id) as id from hxtable group by licenseID having count(*) > 1
) as b
where a.licenseID = b.licenseID and a.id > b.id

同理,如果要保留最新的ID,可以这样写:

delete hxtable as a from hxtable a,
(
select licenseID,max(id) as id from hxtable group by licenseID having count(*) > 1
) as b
where a.licenseID = b.licenseID and a.id < b.id

发表评论
表情
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
昵称   密码   游客无需密码
网址   电邮   [注册]