in Tech

Delete From Two Or More Tables With One MySQL Query

You can delete from multiple tables (which don’t have foreign keys set up) using one query if you create it using the following syntax:

DELETE a.*, b.* 
FROM [table1] as a, [table2] as b
WHERE a.[idcol] = b.[idcol]
AND a.[idcol] = 9; 

You could even delete from more than 2 tables…

DELETE a.*, b.*, c.*
FROM [table1] as a, [table2] as b, [table3] as c
WHERE a.[idcol] = b.[idcol]
AND b.[idcol2] = c.[idcol2]
AND a.[idcol] = 5;

That should save you some writing a fair bit of sql. Happy (and careful) deleting.

Helpful? Say thank you.

Help me keep these guides up to date by showing your apprechiates with a small donation. Scan the QR code below, or buy me a coffee here.

Comments are closed.