2014-03-13 50 views
0

我有这些查询:如何在IN语句中使用另一个查询?

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

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

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

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

但是出错了。谢谢!

回答

1

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

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

你不能在in子句中组合一个值列表和一个子查询。

你要么需要使用一个OR

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

,或者使用一个单一的选择,工会的子查询和静态值

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