我正在使用这些SQL查询从两个表中删除行:
DELETE FROM AGENT WHERE ID = ?
DELETE FROM AGENT WHERE AGENT_GROUP_ID = (SELECT ID FROM AGENT_GROUP WHERE NAME = ?)
我感兴趣的是,我能以某种方式将两个SQL查询合并到一个SQL查询中吗?
答案 0 :(得分: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 :(得分: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 ...)