删除具有相同文本的重复行
Delete duplicate rows with same text
我有 table news
字段:
idNews
idArea
title
text
date
我想要做的是删除所有具有相同 title
和 text
的重复行,除了一个(最早日期的那个)我已经尝试了一些查询但没有成功。
我尝试了这些查询,但没有用:
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;
其中一种方法是
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;
select * from news where title in (
select title from news group by title having count(*) > 1
)
假设 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 不允许您从连接中直接引用的 table 中删除数据。二级子查询创建一个允许删除的临时结果集。
我有 table news
字段:
idNews
idArea
title
text
date
我想要做的是删除所有具有相同 title
和 text
的重复行,除了一个(最早日期的那个)我已经尝试了一些查询但没有成功。
我尝试了这些查询,但没有用:
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;
其中一种方法是
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;
select * from news where title in (
select title from news group by title having count(*) > 1
)
假设 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 不允许您从连接中直接引用的 table 中删除数据。二级子查询创建一个允许删除的临时结果集。