删除Mysql表中的重复记录

Eddy 发布于2013-2-22 15:31:39 分类: 技术心得 已浏览loading 网友评论0条 我要评论

 如何删除Mysql表中的重复记录呢?最容易想到的直接办法(假设我想删除字段orderNum中的重复项):

delete from cbfinancials where id in

(select id from cbfinancials group by orderNum having count(1)>1);

但遗憾的是这样写Mysql会提示错误:You can't specify target table 'cbfinancials ' for update in FROM clause

我们可以这样来解决:

delete from cbfinancials where id in

(select tmp.id from (select * from cbfinancials) as tmp

group by orderNum having count(1)>1);

把两个表分开,第二个用临时表,避免select和delete为同一个表,这样就不会报错了。

已经有(0)位网友发表了评论,你也评一评吧!
原创文章如转载,请注明:转载自Eddy Blog
原文地址:http://www.rrgod.com/technique/870.html     欢迎订阅Eddy Blog

关于 Mysql  的相关文章

记住我的信息,下次不用再输入 欢迎给Eddy Blog留言