Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - Delete from multiple tables using a UNION?

Tags:

mysql

I have tables all with the same columns and have divided them up this way to keep them small.

e.g. Tables - cpus, gfxcards, monitors. Columns - titleId, categoryId, name.

What I'm trying to do is similar to this

SELECT * FROM cpus
UNION
SELECT * FROM gfxcards;
DELETE FROM test WHERE (titleId,categoryId) IN ((2,0));

Is there a way to do this or do I have to use transactions to avoid multiple delete queries?

like image 497
Ken Avatar asked Sep 15 '26 15:09

Ken


1 Answers

Ah, nevermind. I worked out the query I was looking for. e.g. To delete CPUs with IDs 4,7 from 'cpus' table and monitors with IDs 105,106 from 'monitors' table, the following single query works.

DELETE
c, m FROM cpus c, monitors m
WHERE
(c.titleId,c.categoryId) IN ( (4,0), (7,0) )
AND
(m.titleId,m.categoryId) IN ( (105,3), (106,3) );

But thanks for the replies =)

like image 83
Ken Avatar answered Sep 18 '26 04:09

Ken



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!