今日,突然發現雖然經常使用mysql delete操作,但是大多數的時候只是最簡單的單表刪除,恰巧今日碰到了一個應用場景,即需要刪除表內的一些數據,但是一些必要的條件卻是存儲在另一表內,當時一瞬間的想法就是join。
想到就做,構建最原始的delete語句:
delete from table1 where table1.id = 1;
如果需要關聯其他表進行刪除,執行如下操作:
delete table1 from table1 inner join table2 on table1.id = table2.id where table2.type = 'something' and table1.id = 'idnums';
當然,這樣也可實現兩表刪除:
delete table1,table2 from table1 inner join table2 on table1.id = table2.id where table2.type = 'something' and table1.id = 'idnums'