2

First query

    select id from posts where post_title='abc' || post_title='xyz' order by id desc limit 1;

Let's say the returned values are 730 and 735.

Next query

    delete from posts where id in(730,735);

I want both of these queries to be combined into one statement. How can it be done. Please help

I have tried this one below. it doesn't work.

delete from posts where id in
        (
            select id from posts where post_title='abc' order by id desc limit 1,
                            select id from posts where post_title='xyz' order by id desc limit 1
                        );
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Krish Gowda
  • 193
  • 2
  • 17

6 Answers6

2

In my opinion, We must use IF-EXISTS clause when executing delete-and-select in one query, because if select returns null, it will throw an exception so try this:

IF EXISTS (SELECT id FROM [Posts] WHERE post_title IN ('abc', 'xyz'))
BEGIN 
     DELETE FROM posts 
     WHERE id IN (SELECT id 
                  FROM [Posts]
                  WHERE post_title IN ('abc', 'xyz') 
                  ORDER BY post_title, id DESC
                 ) 
END
Sohail
  • 574
  • 3
  • 21
1

Try this:

DELETE FROM posts 
WHERE id IN (SELECT id 
             FROM (SELECT post_title, MAX(id) id 
                   FROM posts 
                   WHERE post_title IN ('abc', 'xyz') 
                   GROUP BY post_title 
                  ) A 
            )

OR

DELETE FROM posts 
WHERE id IN (SELECT id 
             FROM (SELECT post_title, id 
                   FROM posts 
                   WHERE post_title IN ('abc', 'xyz') 
                   ORDER BY post_title, id DESC
                 ) A 
            GROUP BY post_title)
Saharsh Shah
  • 28,687
  • 8
  • 48
  • 83
  • i am trying to delete duplicate entries. so i want only one row returned with highest id per match. – Krish Gowda Dec 26 '13 at 10:57
  • @KrishGowda You will get latest(only one) entry of posts based on post title specified in where condition – Saharsh Shah Dec 26 '13 at 10:58
  • @KrishGowda You're most welcome. You can also upvote any question and answer if you find that it is helpful to you by clicking on up arrow above the right mark – Saharsh Shah Dec 26 '13 at 11:02
0

Try this:

delete from posts where id in (
    select * from
    (
       select id from posts 
       where post_title='abc' or post_title='xyz'
    ) as t
);

You have to wrap it in an alias.

Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
0

As a solution to your problem please try executing following sql query

delete FROM `posts` WHERE  id in 
(select p.id from 
                 (select id from posts where title in('abc','xyz') p)
Rubin Porwal
  • 3,736
  • 1
  • 23
  • 26
0

As explained here

In MySQL, you can't modify the same table which you use in the SELECT part.

If you absolutely need the subquery, there's a workaround, but it's ugly for several reasons, including performance:

delete from posts where id in
(
  select id from (select * from posts)as x 
  where post_title = 'xyz' or
        post_title = 'abc'
  order by id desc
);

see here in action

just add limit 1 in your actual code, not supported by the online version.

Community
  • 1
  • 1
Harry
  • 1,572
  • 2
  • 17
  • 31
0

Try this

DELETE FROM [posts]
WHERE id = ANY( SELECT id FROM [posts] WHERE post_title IN('abc','xyz') );
Sohail
  • 574
  • 3
  • 21
Ajitha Ms
  • 545
  • 5
  • 18