【发布时间】:2019-04-25 02:57:34
【问题描述】:
我想将一个变量从主查询传递到子查询。这是一个问题,因为此查询使用 3 个潜艇。
我试过加入,但因为我是新人,所以有点混乱。代码是这样的
select fav.cust_id
from
(
select cust_id
from
(
select cust_id
from
(
select c.cust_id
from customer c
)
)
)fav
where fav.cust_id = 12;
正如我们所见,我试图将值“12”传递给最深的子查询 (c.cust_id),以便它在主查询中返回预期值。如果我尝试在第一个子查询中传递值,它将返回错误数据,因为它会在使用条件之前尝试从第二个子查询中获取所有数据。所以我想要做的是在最深的子查询中传递条件,以便它从那里过滤结果以返回预期的值。
更新: 这是我所做的接近真实的查询。
select fav.cust_lname, fav.cust_time
from
(
select max(cust_lname), max(cust_time)
--there is another code here for some calculations
from
(
select lname cust_lname, time cust_time
--there is another code here for some calculations
from
(
select c.cust_id
--there is another code here for some calculations to return the cust_lname and cust_time
from
customer c
where cust_g = 'MALE'
AND cust_id = --in the original code, this is a parameter. i want this to read the value from the main query
)
)
)fav,
customer_table ct
where ct.header_id = --custom parameter that im trying to play with
AND ct.cust_id = --i want to relate this with the cust_id inside the sub query
【问题讨论】:
-
如果我没记错的话,这个查询可以简化为
select 12*1 from dual; -
内部子查询应该与外部表连接。在示例中你刚刚提到了一个表......但实际上我猜必须有多个表......在这种情况下,内部查询和表应该有相应的连接列......在连续的内部查询中,这也必须是跟随...尝试...没有适当的示例或表格,...我们无法构建示例查询
-
@Tamil hye 我已经更新了查询
标签: sql oracle correlated-subquery