根据条件删除 SQL 数据库中的重复项

Removing duplicates in an SQL database on a condition

我需要从 table 中删除重复项,如下所示:

id           post_author    post_title
-----------------------------------------------------------------------------
21319        1              Youngstown State University
20535        1              Yo San University of Traditional Chinese Medicine
30268        29             Yo San University of Traditional Chinese Medicine
29747        29             Yeshiva University
21964        1              Yale University
29247        29             Yale University
29497        29             Xavier University of Louisiana
21916        1              Xavier University
29862        29             Xavier University
29860        29             Wright State University-Main Campus
20915        1              Wright State University-Lake Campus
21562        1              World Mission University
30267        29             World Mission University

基本上,如果有两个条目具有相同的 post_title,我需要删除具有 post_author = 1 的条目,但如果 post_title 是唯一的,则应保留该条目原样。

如何使用 SQL 查询完成此操作?

编辑:

我试过 Mureinik 建议的查询。查询如下所示:

DELETE t FROM wp_posts AS t 
WHERE t.post_author = 1 AND
EXISTS (SELECT * FROM wp_posts s 
                WHERE t.post_title = s.post_title
                AND s.post_authot != 1)

但是我得到了错误:

[Err] 1093 - You can't specify target table 't' for update in FROM clause

我做错了什么?

您可以使用 exists 运算符:

DELETE FROM my_table t
WHERE  post_author = 1 AND
       EXISTS (SELECT *
               FROM   my_table s
               WHERE  t.post_title = s.post_title AND
                      s.post_author != 1)

使用 in 运算符:

delete from TableName where post_author = 1
and post_title in(select post_title from TableName group by post_title having count(*) > 1)

您可以在 post_author = 1 上设置条件,其中有多个 post_title 具有临时 table:

CREATE TEMPORARY TABLE t_del AS SELECT post_title 
                     FROM t 
                     GROUP BY post_title 
                     HAVING count(post_title)>1;               

DELETE FROM t 
WHERE post_author = 1 
  AND post_title IN (select post_title FROM t_del) ;

SQL Fiddle here