0

im trying to execute this query but i'm getting error #1064.

delete from tableABC WHERE ID in (select ID from `TABLEXYZ` where `qty` = 0);

But the following statement works

select * from tableABC WHERE ID in (select ID from TABLEXYZ where qty = 0);

2
  • then how about delete from tableABC WHERE ID in (select ID from TABLEXYZ where qty = 0); Commented Dec 6, 2014 at 6:34
  • what is the differences between my query? it's just lack of the backtick Commented Dec 6, 2014 at 7:37

1 Answer 1

2

Here is the answer:

to use a delete query with subquery which has a where condition, do the following:

DELETE a FROM `tableABC` AS a JOIN (SELECT ID FROM `tableXYZ` WHERE `qty`=0) as b on b.ID = a.ID
Sign up to request clarification or add additional context in comments.

1 Comment

THis is right -- the key part is that by naming the subquery 'b' you force it to get persisted to a temporary table (invisibly handled by the sql engine). To do deletes using a subquery, it's necessary that the subquery gets temporarily persisted.

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.