Possible Duplicate:
Remove Duplicate Rows Leaving Oldest Row Only?
我有一个posts_table
在我的数据库中..
该 posts_table 具有字段 post_title
, post_id
,(以及其他一些不重要的)。
如果 post_title 在另一行中重复,我需要删除其中一行。
示例:
posts_table
-------------------------------------------
post_id | post_title
-------------------------------------------
501 | Some post title here
502 | Another post title
503 | A test post tile
504 | Some post title here (this is duplicated, i need to delete this row)
505 | A different post title
通过下面的句子,我可以检查所有重复的 post_titles
SELECT post_title, COUNT(post_title) AS dup_count
FROM posts_table
GROUP BY post_title
HAVING (COUNT(post_title) > 1)
问题是,如何删除具有重复 post_titles 的所有行?
最好、最快的查询是:
delete from posts_table where post_id in (
select post_id from (
select post_id from posts_table a group by post_title having count(post_title) > 1
) b
)
最佳答案
试试这个:
DELETE FROM Posts_Table
WHERE Post_ID NOT IN
(
SELECT a.Post_ID
FROM
(
SELECT Post_Title, Post_ID
FROM Posts_Table
GROUP BY Post_Title
) a
)
关于mysql - 如果字段 "post_title"在另一行中重复,如何删除表行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9196884/