我有一个表,其中一列 title 中有重复的值,因此 title 列有多个具有相同值的行。
我想删除所有重复项,但标题相同。
我可以执行哪种查询来完成此操作?
Title Subject Description Created_at
Something Somethingsubject Somethingdescription 2016-04-13 16:37:10
Something Anothersubject Anotherdescription 2016-04-11 16:37:10
Something Thirdsubject Thirdsubject 2016-04-14 16:37:10
NumberTwo NumberTwoSubject NumberTwoSubject 2016-04-12 16:37:10
NumberTwo AnotherNumberTwo AnotherNumberTwoDescripti 2016-04-15 16:37:10
我想删除所有重复项,只留下一个,最好是最旧的记录,这样唯一剩下的记录就是:
Title Subject Description Created_at
Something Anothersubject Anotherdescription 2016-04-11 16:37:10
NumberTwo NumberTwoSubject NumberTwoSubject 2016-04-12 16:37:10
最佳答案
你可以做一个自连接DELETE
:
DELETE t1
FROM mytable t1
JOIN (SELECT Title, MAX(Created_at) AS max_date
FROM mytable
GROUP BY Title) t2
ON t1.Title = t2.Title AND t1.Created_at < t2.max_date
关于mysql - 如何使用 MySQL 删除一列中所有重复的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36922940/