删除具有相同文本的重复行

时间:2015-07-27 11:57:31

标签: mysql sql text duplicate-removal

我有这个字段的表news

idNews idArea title text date

我想要做的是删除所有具有相同titletext的重复行,除了一个(具有最早日期的行)我已经尝试了一些查询而没有成功。

我尝试了这些查询,但它们无效:

DELETE FROM news WHERE idNews NOT IN (SELECT MIN(date) FROM news GROUP BY title,text, date); 

DELETE idNews FROM news WHERE date< date AND title= title and text=text;

3 个答案:

答案 0 :(得分:0)

假设idNews是关键,那么这应该有效:

delete from news 
where idnews not in (
  select idnews from (
    select idnews from news
    join (
      select title, text, min(date) as min_date 
      from news
      group by title, text
    ) x 
     on news.title = x.title
    and news.text  = x.text 
    and news.date  = x.min_date
  ) a
);

嵌套连接的原因是MySQL不允许您从您在连接中直接引用的表中删除数据。第二级子查询创建一个允许删除的临时结果集。

Sample SQL Fiddle

答案 1 :(得分:-1)

其中一种方法是

delete from table as t1 inner join
(
select title,text,min(date) as date from table group by title,text
) as t2 on t1.title=t2.title and t1.text=t2.text 
where t1.date>t2.date;

答案 2 :(得分:-1)

select * from news where title in (
    select title from news group by title having count(*) > 1
)