mysql delete 语句(连接删除)

今日,突然发现虽然经常使用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'


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章