MySQL多表關(guān)聯(lián)數(shù)據(jù)同時(shí)刪除sql語(yǔ)句 有需要的朋友可參考。
DELETE刪除多表數(shù)據(jù),怎樣才能同時(shí)刪除多個(gè)關(guān)聯(lián)表的數(shù)據(jù)呢?這里做了深入的解釋:
代碼如下 |
復(fù)制代碼 |
1 delete from t1 where 條件
2 delete t1 from t1 where 條件
3 delete t1 from t1,t2 where 條件
4 delete t1,t2 from t1,t2 where 條件 |
category(欄目信息表)和news(新聞數(shù)據(jù)表)。
category中的id(欄目編號(hào))字段作為該表的主鍵(primary key).唯一標(biāo)識(shí)了一個(gè)欄目的信息。
news 中的id字段作為該表的主鍵(primary key).唯一標(biāo)識(shí)了一個(gè)欄目的信息。
category_id(欄目編號(hào))字段與category表的id字段相關(guān)聯(lián)。
1.SQL刪除語(yǔ)句
代碼如下 |
復(fù)制代碼 |
delete category,news from category left join news on category.id = news.category_id
|
1、從數(shù)據(jù)表t1中把那些id值在數(shù)據(jù)表t2里有匹配的記錄全刪除掉1
代碼如下 |
復(fù)制代碼 |
DELETE t1 FROM t1,t2 WHERE t1.id=t2.id 或 DELETE FROM t1 USING t1,t2 WHERE t1.id=t2.id
|
2、從數(shù)據(jù)表t1里在數(shù)據(jù)表t2里沒(méi)有匹配的記錄查找出來(lái)并刪除掉1
代碼如下 |
復(fù)制代碼 |
DELETE t1 FROM t1 LEFT JOIN T2 ON t1.id=t2.id WHERE t2.id IS NULL 或 DELETE FROM t1,USING t1 LEFT JOIN T2 ON t1.id=t2.id WHERE t2.id IS NULL
|
3、 從兩個(gè)表中找出相同記錄的數(shù)據(jù)并把兩個(gè)表中的數(shù)據(jù)都刪除掉1
代碼如下 |
復(fù)制代碼 |
DELETE t1,t2 from t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t1.id=25 |
注意此處的delete t1,t2 from 中的t1,t2不能是別名
如:1
代碼如下 |
復(fù)制代碼 |
delete t1,t2 from table_name as t1 left join table2_name as t2 on t1.id=t2.id where table_name.id=25
|
在數(shù)據(jù)里面執(zhí)行是錯(cuò)誤的(MYSQL 版本不小于5.0在5.0中是可以的)
上述語(yǔ)句改 寫成1
代碼如下 |
復(fù)制代碼 |
delete table_name,table2_name from table_name as t1 left join table2_name as t2 on t1.id=t2.id where table_name.id=25MySQL多表關(guān)聯(lián)數(shù)據(jù)同時(shí)刪除sql語(yǔ)句 有需要的朋友可參考。
DELETE刪除多表數(shù)據(jù),怎樣才能同時(shí)刪除多個(gè)關(guān)聯(lián)表的數(shù)據(jù)呢?這里做了深入的解釋:
代碼如下 |
復(fù)制代碼 |
1 delete from t1 where 條件
2 delete t1 from t1 where 條件
3 delete t1 from t1,t2 where 條件
4 delete t1,t2 from t1,t2 where 條件 |
category(欄目信息表)和news(新聞數(shù)據(jù)表)。
category中的id(欄目編號(hào))字段作為該表的主鍵(primary key).唯一標(biāo)識(shí)了一個(gè)欄目的信息。
news 中的id字段作為該表的主鍵(primary key).唯一標(biāo)識(shí)了一個(gè)欄目的信息。
category_id(欄目編號(hào))字段與category表的id字段相關(guān)聯(lián)。
1.SQL刪除語(yǔ)句
代碼如下 |
復(fù)制代碼 |
delete category,news from category left join news on category.id = news.category_id
|
1、從數(shù)據(jù)表t1中把那些id值在數(shù)據(jù)表t2里有匹配的記錄全刪除掉1
代碼如下 |
復(fù)制代碼 |
DELETE t1 FROM t1,t2 WHERE t1.id=t2.id 或 DELETE FROM t1 USING t1,t2 WHERE t1.id=t2.id
|
2、從數(shù)據(jù)表t1里在數(shù)據(jù)表t2里沒(méi)有匹配的記錄查找出來(lái)并刪除掉1
代碼如下 |
復(fù)制代碼 |
DELETE t1 FROM t1 LEFT JOIN T2 ON t1.id=t2.id WHERE t2.id IS NULL 或 DELETE FROM t1,USING t1 LEFT JOIN T2 ON t1.id=t2.id WHERE t2.id IS NULL
|
3、 從兩個(gè)表中找出相同記錄的數(shù)據(jù)并把兩個(gè)表中的數(shù)據(jù)都刪除掉1
代碼如下 |
復(fù)制代碼 |
DELETE t1,t2 from t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t1.id=25 |
注意此處的delete t1,t2 from 中的t1,t2不能是別名
如:1
代碼如下 |
復(fù)制代碼 |
delete t1,t2 from table_name as t1 left join table2_name as t2 on t1.id=t2.id where table_name.id=25
|
在數(shù)據(jù)里面執(zhí)行是錯(cuò)誤的(MYSQL 版本不小于5.0在5.0中是可以的)
上述語(yǔ)句改 寫成1
代碼如下 |
復(fù)制代碼 |
delete table_name,table2_name from table_name as t1 left join table2_name as t2 on t1.id=t2.id where table_name.id=25 | |
|