Mysql 中多表關聯刪除,其中連線表為空值

2021-09-26 15:54:46 字數 497 閱讀 1036

首先三張表 a,b,c

它們之間的關係為b表中有a表的主鍵c表中有b表的主鍵

就是說 a表與b表 為 一對多 b表與c表為 一對多 b表就是中間表

首先我就想到了連線,這裡不多說,直接上**:

delete

a,b,c

from a

left join b

ona.a_id=b.a_id

left join

c on

b.b_id=c.b_id

where

a.a_id='1'

使用 left join 也可以解決 b表或者c表中沒有資料時,也可以將a表中的內容刪除掉

使用inner join b表或者c表中沒有資料時,a表中的資料就刪除不了。

如果是兩張表的話 也可以用上面的方法,可以直接套用。

mysql 多表關聯刪除

兩張表關聯刪除 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 三張...

mysql 多表關聯刪除

sql檔案 set foreign key checks 0 table structure for stu tea drop table ifexists stu tea create table stu tea stu id int 11 not null,tea id int 11 not n...

oracle 多表刪除 同時刪除多表中關聯資料

oracle 多表刪除 同時刪除多表中關聯資料 1 從資料表t1中把那些id值在資料表t2裡有匹配的記錄全刪除掉 delete t1 from t1,t2 where t1.id t2.id 或delete from t1 using t1,t2 where t1.id t2.id 2 從資料表t1...