【问题标题】:how to pass value from main query inside a sub sub sub query如何在子子查询中从主查询传递值
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    根据您给定的查询,

    (
        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 的代码 sn-p.... 从哪里获取 ct.cust_id ???它的 ct 表不在子查询中,这将给出无效标识符错误。

    根据您给定的代码,您尝试通过与外部客户表中的 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...
    

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

    主要是您使用了表内查询与外部表的连接,这在表内查询中不可用,要么切换到子查询,要么在表内查询中引入外部表

    【讨论】:

      【解决方案3】:

      好的,所以我今天学到了新东西。我使用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
      

      【讨论】:

        猜你喜欢
        • 2013-09-24
        • 2014-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多