我有一个表在一列中有重复值,标题,因此标题列有多个具有相同值的行.
我想删除除标题相同之外的所有重复项.
我可以执行什么样的查询来完成此任务?
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
Run Code Online (Sandbox Code Playgroud)
我想删除所有重复项,只留下一个,最好是最旧的记录,以便剩下的唯一记录是:
Title Subject Description Created_at
Something Anothersubject Anotherdescription 2016-04-11 16:37:10
NumberTwo NumberTwoSubject NumberTwoSubject 2016-04-12 16:37:10
Run Code Online (Sandbox Code Playgroud)
你可以做一个自我加入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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
48 次 |
| 最近记录: |