【问题标题】:Oracle select query with dynamic in clause带有动态 in 子句的 Oracle 选择查询
【发布时间】:2013-07-26 08:51:33
【问题描述】:

我有以下问题

    select * from table_1 
    where Conditions in 
         (select case when check_condition = 'Y' then 'Condition_1' 
                 else 'N/A' end as Check_condition 
          from table_2 
          WHERE id = 1122)

其中 table_1 包含列 Conditions 中的值,如下所示。 条件_1,条件_2

这很好用,然后将结果返回给我。

我想在in 子句中使用多个选择语句,我按如下方式进行。

    select * from table_1
    where Conditions in (
         select ''''||
             (select case when check_condition = 'Y' then 'Condition_1' 
               else 'N/A' end as Check_condition 
               from table_2 
               WHERE id = 1122)||''''||','''||
                  (select case when check_condition = 'Y' then 'Condition_2'
                        else 'N/A' end as Check_condition 
                  from table_2 WHERE id = 1122)||''''
                 from dual
                )

内部查询(在 in 子句内)按预期给我正确的结果 -

'Condition_1','Condition_2' 

当我将其复制粘贴到父查询时,它可以正常工作并显示结果。

select * from table_1 where Conditions in ('Condition_1','Condition_2')

我的问题是,当我使用第二个查询时,它没有给出任何结果。我知道子查询将返回与外部查询中的行匹配的结果。但它显示了空的结果集。

我正在使用 oracle 11g

谁能帮帮我..提前谢谢大家。

【问题讨论】:

    标签: sql database oracle oracle11g


    【解决方案1】:

    关于要求的问题有点不清楚。我认为您想要的是仅在以下情况下从table1 中选择记录:

    • 行匹配 'Condition_1' 或 'Condition_2'
    • check_condition = 'Y'
    • table2 中有一行 ID = 1122

    从您的问题中不清楚check_condition 是列还是变量,以及它属于哪个表的列。因此,这个解决方案可能是错误的,但它说明了原理。

    select * from table1 t1
    where t1.conditions in ('Condition_1','Condition_2')
    and t1.check_condition = 'Y'
    and exists
            ( select null from table2 t2
              where t2.id = 1122 )
    

    如果这不能提供您需要的解决方案,请修改您的问题,以便说明您需要实现的业务逻辑,并包括相关的表格描述。

    【讨论】:

    • 非常感谢您的回答。我明白了。会努力的。感谢您的帮助。
    【解决方案2】:

    您不会像手动执行时那样将两个值传递到 in 子句中:

    select * from table_1 where Conditions in ('Condition_1','Condition_2')
    

    您传递的是单个值,它是值的串联:

    select * from table_1 where Conditions in ('''Condition_1'',''Condition_2''')
    

    并且没有condition 匹配该连接值,因此您不会得到任何结果。你可以这样做:

    select * from table_1 where Conditions in (
      select case when check_condition = 'Y' then 'Condition_1' else 'N/A' end
      from table_2 WHERE id = 1122
      union all
      select case when check_condition = 'Y' then 'Condition_2' else 'N/A' end
      from table_2 WHERE id = 1122
    )
    

    或者可能,如果我按照您的操作进行操作(这是值得怀疑的,因为我不确定我是否了解您的数据模型!):

    select * from table_1 where check_condition != 'Y' or Conditions in (
      select 'Condition_1' from table_2 WHERE id = 1122
      union all
      select 'Condition_2' from table_2 WHERE id = 1122
    )
    

    您似乎应该能够通过连接更简洁地执行此操作,但我认为我们需要查看结构和示例数据以进一步了解发生了什么。

    【讨论】:

    • 非常感谢您的澄清。我会研究你的方法。感谢您的帮助。
    • @sam - 我认为 APC 的方法可能更接近您的需要(当然更好,因为它只触及了一次 table2),但我将把它留在这里解释部分。
    猜你喜欢
    • 2014-03-08
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 2018-03-08
    • 2014-05-02
    • 2013-11-15
    相关资源
    最近更新 更多