MySql删除重复记录(亲自测试过)

来源:互联网 发布:qq教程网源码 编辑:程序博客网 时间:2024/06/05 06:40

刚开始在网上查找相关语句,发现运行有问题 

     最开始的语句  :

DELETE FROM laugh 
WHERE detail_url  IN (SELECT  detail_url  FROM laugh  GROUP  BY  detail_url   HAVING  COUNT(detail_url) > 1)
AND catch_time NOT IN (SELECT MIN(catch_time)AS catch_time FROM  laugh    GROUP BY detail_url  HAVING COUNT(detail_url )>1)

说明:catch_time代表唯一标示laugh  这张表的一个子段

运行后报错:You can't specify target table 'laugh  ' for update in FROM clause

查找解决方法:

原因是:mysql中不能这么用。 (等待mysql升级吧)。那串英文错误提示就是说,不能先select出同一表中的某些值,

再update这个表(在同一语句中)。 也找到替代方案,重写改写了 sql 。

DELETE FROM laugh 
WHERE detail_url  IN (SELECT a.detail_url FROM (SELECT  detail_url  FROM laugh  GROUP  BY  detail_url   HAVING  COUNT(detail_url) > 1)a)
AND catch_time NOT IN (SELECT b.catch_time FROM (SELECT MIN(catch_time)AS catch_time FROM  laugh    GROUP BY detail_url  HAVING COUNT(detail_url )>1)b)

再运行OK 了。本人亲自测试过的

原创粉丝点击