Frank Vilea
Frank Vilea

Reputation: 8487

MySQL: DELETE FROM with information from a subquery

I have a table delete_requests where I store the id's for delete requests for the entries of my users table. Is it possible to delete from users with the information from delete_requests as a condition?

My problem is, that the result set will usually not be limited to one row but return several.

DELETE FROM users WHERE id=(SELECT id FROM delete_requests)

So MySQL complains:

#1242 - Subquery returns more than 1 row

Can this be done in one statement without putting the logic into the executing application?

Upvotes: 0

Views: 2020

Answers (3)

Bryan
Bryan

Reputation: 1241

DELETE FROM users WHERE id IN (SELECT id FROM delete_requests)

Upvotes: 2

Shakti Singh
Shakti Singh

Reputation: 86466

Use the IN clause

DELETE FROM users WHERE id IN (SELECT id FROM delete_requests)

Upvotes: 5

xdazz
xdazz

Reputation: 160943

You can use the in clause.

DELETE FROM users WHERE id IN (SELECT id FROM delete_requests)

Upvotes: 2

Related Questions