两张表关联删除:
DELETE a,b FROM table1 a INNER JOIN table2 b ON a.id = b.aid WHERE a.id = '1' //或者也可以 DELETE a,b FROM table1 a,table2 b WHERE a.id = b.aid AND a.id = '1'
三张表删除
DELETE a,b,c FROM table1 a INNER JOIN table2 b ON a.id = b.aid INNER JOIN table3 c ON a.id = c.aid WHERE a.id = '1'
不过这样有一个问题,就是如果a表里数据,而b表或者c表里没数据,那么整个删除就失败,即删除0条数据
如果你的主表一定有数据,而关联的表有可能有数据也有可能没数据的话,我们可以通过左连接删除的方式,把两张表都删除。无论关联的表有没有数据,主表都可以删除成功
DELETE a.* ,b.* ,c.* FROM table1 a LEFT JOIN table2 b ON a.id = b.aid LEFT JOIN table3 c ON a.id = c.aid WHERE a.id = 1