如何在IN语句中使用另一个查询?

时间:2014-03-13 18:14:56

标签: sql db2

我有这些疑问:

 Select CODE
 FROM TABLE.STATUS
 WHERE REFERENCE IN ('423',{variable})

Select Sec_Code
From Table.Sec_status
Where Sec_reference = 'ABC'

第二个查询返回35个三位数代码,我想插入我试过的{variable}部分:

 Select CODE
 FROM TABLE.STATUS
 WHERE REFERENCE IN ('423',(Select Sec_Code
 From Table.Sec_status
 Where Sec_reference = 'ABC'))

但这是错误的。谢谢!

2 个答案:

答案 0 :(得分:3)

您无法在in子句中组合值列表和子查询。

您需要使用OR

WHERE REFERENCE = `423` or 
      REFERENCE IN (Select Sec_Code
                From Table.Sec_status
                Where Sec_reference = 'ABC')

或使用单个SELECT来管理您的子查询和静态值

WHERE REFERENCE IN (Select Sec_Code 
 From Table.Sec_status
 Where Sec_reference = 'ABC'
UNION ALL
Select '423' 
from SYSIBM.SYSDUMMY1)

答案 1 :(得分:1)

您需要将任何硬编码的数字组合到子查询中。试试这个:

 Select CODE
 FROM TABLE.STATUS
 WHERE REFERENCE IN (
   Select Sec_Code
   From Table.Sec_status
   Where Sec_reference = 'ABC'
   UNION '423'
 )