0

I have table news with this fields:

idNews idArea title text date

What I want to do is delete all duplicate rows with same title and text except one (the one with the earliest date) i already try some query without success.

I tried these queries, but they didn't work:

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;
jpw
  • 44,361
  • 6
  • 66
  • 86
Daggra
  • 3
  • 3

3 Answers3

0

Assuming idNews is a key then this should work:

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
);

The reason for the nested joins is that MySQL won't let you delete data from a table that you're directly referencing in a join. The second level subquery creates a temporary result set that will allow the delete.

Sample SQL Fiddle

jpw
  • 44,361
  • 6
  • 66
  • 86
-1

One of the methods is

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;
Madhivanan
  • 13,470
  • 1
  • 24
  • 29
-1
select * from news where title in (
    select title from news group by title having count(*) > 1
)
code19
  • 75
  • 4