重用select查询结果oracle

时间:2016-08-08 14:47:16

标签: sql oracle

我有以下查询

SELECT ID FROM MARMELADES mrm 
where not exists 
    (SELECT 1 FROM TOYS toys 
    WHERE mrm.ID = toys.ID 
    AND mrm.INGREDIENT = toys.INGREDIENT 
    AND mrm.BOX_TYPE = 2)
AND mrm.BOX_TYPE = 2 

它会返回近400多个id结果,例如[12, 33, 45, ... , 3405]

现在,我想从我的数据库中删除该列表中的所有ID。这不仅仅是MARMELADES和TOYS。另外,我有35个以上的桌子,我可以有这个ID。

如果这个查询可以在ALL_UNNEEDED_IDS等函数中提取,我会很高兴,所以我可以像这样使用它:

DELETE FROM ANOTHER_TABLE_1 WHERE ID IN ( ALL_UNNEEDED_IDS ) 
DELETE FROM ANOTHER_TABLE_2 WHERE ID IN ( ALL_UNNEEDED_IDS ) 
DELETE FROM ANOTHER_TABLE_3 WHERE ID IN ( ALL_UNNEEDED_IDS ) 
DELETE FROM ANOTHER_TABLE_4 WHERE ID IN ( ALL_UNNEEDED_IDS ) 
...
DELETE FROM ANOTHER_TABLE_35 WHERE ID IN ( ALL_UNNEEDED_IDS ) 

可以在oracle中重复使用这样的结果吗?

1 个答案:

答案 0 :(得分:0)

在后续查询中使用第一个查询。 IE:

DELETE FROM ANOTHER_TABLE_1 WHERE ID IN (
     SELECT ID FROM MARMELADES mrm 
     where not exists 
         (SELECT 1 FROM TOYS toys 
         WHERE mrm.ID = toys.ID 
         AND mrm.INGREDIENT = toys.INGREDIENT 
         AND mrm.BOX_TYPE = 2)
     AND mrm.BOX_TYPE = 2
);

当你到达玩具和marmelades牌桌时,你需要一个临时的牌桌,正如@Gordon建议的那样。