3

I need to remove duplicates from a table that looks like this:

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

Basically, if there are two entries with the same post_title, I need to remove the one with post_author = 1, but if the post_title is unique then the entry should be left as is.

How can this be done with an SQL query?

EDIT:

I've tried a query suggested by Mureinik. The query looks like this:

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)

But I got error:

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

What am I doing wrong?

3
  • 1
    is this select or delete?? Commented May 15, 2015 at 10:08
  • 1
    Is it on mysql or postgresql? Commented May 15, 2015 at 10:09
  • It's delete, I need to remove duplicates. It's for MySQL Commented May 18, 2015 at 11:10

3 Answers 3

2

You could use the exists operator:

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)
Sign up to request clarification or add additional context in comments.

1 Comment

I've tried this, but got an error. Please, take a look at the updated question. Any ideas on what I did wrong?
1

You could set a condition on post_author = 1 where there are more than one post_title with a temporary 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

1 Comment

You are right, you need a temporary table. This cannot be done in a single SQL. Check my updated answer
0

With in operator:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.