如何从子子查询中的主查询传递值

问题描述 投票:-2回答:3

我想将主查询中的变量传递给子查询。这是一个问题,因为此查询使用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

sql oracle correlated-subquery
3个回答
0
投票

查询可以简化如下。您不需要嵌套子查询。

select fav.cust_id
from fav
join b
on b.cust_id = fav.cust_id
join c
on c.cust_id = b.cust_id
WHERE fav.cust_id = 12

0
投票

根据您的查询,

(
    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

是表内查询(获取的记录用作table1数据)

使用的另一个表是

customer_table ct

并且根据您的代码,它就像在table1(fav)中加入第二个表一样,但它在子查询中不可用。

考虑这个...仅用于table1的代码片段....从哪里获取ct.cust_id ???它ct表不在子查询中,这会给出无效的标识符错误。

根据您给定的代码,您尝试通过从外部customer表连接cust_id来获取fav.cust_lname,fav.cust_time,它们是子查询中的值。如果这是要求,那么它可以写成

select (subquery with join from ct table) from customer_table ct

如果您想在表内查询中使用连接

select column1, column2 from (select ....cust_id... from customer_table ctin ...)fav, customer_table ct where...

应该这样做,即应该在表内查询中参考该列调用连接表

主要的是你使用了in-table查询加入外部表,这在表内查询中是不可用的,切换到子查询或在表内查询中引入外部表


0
投票

好吧所以我今天学到了新东西。我使用OUTER APPLY来传递最深的子查询中的值,感谢所有的建议。

select fav.cust_lname, fav.cust_time
from
customer_table ct
outer apply
(
    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 = ct.cust_id --match with the column in the outside query table
         )
    )       
)fav,

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 --removed this
© www.soinside.com 2019 - 2024. All rights reserved.