Wwwebber's Blog

How To Delete from Multiple Tables in Mysql

Posted in MySql by wwwebber on August 11, 2009

$query1 = ‘DELETE FROM table1 where id=1’;

$query2 = ‘DELETE FROM table2 where id=1’;

Many MySql newbies don’t know that you can join delete just like select and updates. Here’s how you’d issue a single query in a join

$betterQuery = ‘DELETE t1.*, t2.* FROM table1 t1 INNER JOIN table2 t2 ON t1.id= t2.id  WHERE  t1.id=1’;

Same effect but you’re only query the datbase once rather than twice.

Advertisements

Comments Off on How To Delete from Multiple Tables in Mysql

%d bloggers like this: