2014-08-27 86 views
0

我使用這些SQL查詢來刪除行,兩個表從兩個表中刪除:使用一個SQL查詢

DELETE FROM AGENT WHERE ID = ? 

DELETE FROM AGENT WHERE AGENT_GROUP_ID = (SELECT ID FROM AGENT_GROUP WHERE NAME = ?) 

我感興趣我可以以某種方式結合兩個SQL查詢到一個SQL查詢?

回答

4

像這樣:

DELETE FROM AGENT 
WHERE ID = ? 
    OR AGENT_GROUP_ID = (SELECT ID FROM AGENT_GROUP WHERE NAME = ? 

?根據docs,您不能同時從多個表中刪除記錄。作爲一個解決方案,你可以寫一個存儲過程,會從多個表中刪除記錄,像這樣:

create procedure delete_from_multiple_tables (
    p_key1 number 
, p_key2 varchar2 
) 
as 
begin 
    delete table1 where id = p_key1; 
    delete table2 where group_id = p_key2; 
end delete_from_multiple_tables; 
1

是的,你可以..這是相同的表?如果是的話:

DELETE FROM AGENT WHERE AGENT_GROUP_ID = (SELECT ID FROM AGENT_GROUP WHERE NAME = ?) Or ID = ? 

隨着不同勢表:

DELETE AGENT, MYTABLE2 
FROM AGENT INNER JOIN MYTABLE2 
WHERE AGENT.id = ? OR MYTABLE2.id = (Select ID from ...) 
+0

你的答案是一樣的前面的回答,您並沒有帶來新的想法 – simsim 2014-08-27 08:22:58

+0

不幸的是我使用的是不同的表的SQL查詢。 – user1285928 2014-08-27 08:24:55

+0

@simsim jap ..我沒有看到zaratustra的答案..在我postet後......哦......太晚了.. :) user1285928:好的..我將我的答案延伸 – 2014-08-27 08:38:24