过滤数据库并删除重复

时间:2016-03-01 10:37:23

标签: php mysql sql

我有一个拥有1200000条记录的Mysql数据库,它正在拿着着名艺术家的专辑。唯一的问题是,有重复但唯一的重复字段不是主键而是标题。 PHP中是否有一个简单的脚本来删除不必要的脚本?

2 个答案:

答案 0 :(得分:2)

如果exists另一行具有相同的标题但ID较低,则删除一行。

delete from tablename t1
where exists (select * from tablename t2
              where t2.title = t1.title
                and t2.id < t1.id)

答案 1 :(得分:0)

试试这个

SELECT distinct tablename .*
  FROM tablename as tb1
  join tablename as tb2
 WHERE tb1.title = tb2.title
   and tb1.id < tb2.id

它可能会帮助您获取非重复记录

删除重复记录

DELETE tablename 
  FROM tablename as tb1
  join tablename as tb2
 WHERE tb1.title = tb2.title
   and tb1.id < tb2.id