Delete From Two Or More Tables With One MySQL Query

fetchit-21

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 sql. Happing (careful) deleting.

One comment.

  1. Ultimate Query, and easy to use…

    Thanks