2013-10-15 62 views
1

我拖表SQL內連接刪除

tests_sub,tests_sub_sub

tests_sub.id = tests_sub_sub.id

,所以我想用一個

SQL刪除兩個表中的數據查詢

我用下面的內連接

DELETE tests_sub, tests_sub_sub FROM tests_sub 
INNER JOIN tests_sub_sub ON tests_sub_sub.id = tests_sub.id 
WHERE tests_sub.id = 10 

查詢工作正常只有當兩個表具有項爲tests_sub.id ... (如果tests_sub_sub有test_sub.id = 10沒有進入...雖然表tests_sub有條目爲該ID沒有行受到影響... 請建議一些提示....

+0

請參考此鏈接使用連接刪除 http://stackoverflow.com/questions/652770/delete-with-join-in-mysql – Deepak

+0

使用LEFT JOIN我認爲解決您的問題 –

+0

內連接只產生表A和表B中匹配的記錄集。嘗試使用'Left Outer Join' – heretolearn

回答

1

使用LEFT JOIN

DELETE tests_sub, tests_sub_sub 
FROM tests_sub 
     LEFT JOIN tests_sub_sub 
      ON tests_sub_sub.id = tests_sub.id 
WHERE tests_sub.id = 10 
+0

感謝兄弟....但如何有3個表??? ........ .............. DELETE tests,tests_sub FROM tests LEFT JOIN tests_sub ON tests_sub.idtests = tests.idtests LEFT JOIN tests_sub_sub ON tests_sub_sub.idtests_sub = tests_sub.idtests_sub WHERE tests.idtests = 10 – sx3

+0

t加上'DELETE a,b,c FROM LEFT JOIN b ON .... LEFT JOIN c on .... WHERE ....' - 如果你想刪除3個表格。 –

+0

好的兄弟我明白了..它工作正常..謝謝。 – sx3