gpt4 book ai didi

sql - 如何在oracle中的多选中避免相同的子查询?

转载 作者:行者123 更新时间:2023-12-03 09:51:48 25 4
gpt4 key购买 nike

这是使用相同子查询的三个不同选择。如何使用子查询结果而不是再次执行子查询。

SELECT  *
FROM Address
WHERE address_key IN
(
SELECT address_key
FROM person_address
WHERE peson_key IN (person_list)
); -- person_list := '1,2,3,4'

SELECT *
FROM Phone
WHERE phone_key IN
(
SELECT address_key
FROM person_address
WHERE peson_key IN (person_list)
);

SELECT *
FROM Email
WHERE address_key IN
(
SELECT address_key
FROM person_address
WHERE peson_key IN (person_list)
);

最佳答案

使用 with 子句。我没有重新创建您的确切示例问题,但是可以将任意数量的重复子查询放入 WITH 子句中,然后在查询中引用。

WITH  address_keys as (
SELECT address_key
FROM person_address
WHERE peson_key IN (person_list)
)
Select * from table1, table2, address_keys
where table1.address_key = address_keys.address_key
and table2.address_key = address_keys.address_key

关于sql - 如何在oracle中的多选中避免相同的子查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1074848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com