日期:2014-05-17  浏览次数:20446 次

按条件查询
SQL code
select JO026,* 
FROM 
    [dbo].[APSJOB]
where
    JO015 IN (SELECT RE002 FROM APSRES WHERE RE001 = 'liyu')


我想实现:
JO026 = '0'加JO015 IN (SELECT RE002 FROM APSRES WHERE RE001 = 'liyu')条件限定
JO026 = '1'不加JO015 IN (SELECT RE002 FROM APSRES WHERE RE001 = 'liyu')条件限定

开始我想像下边这样写,显然思路和语法不对

SQL code
select * 
    FROM 
        [dbo].[APSJOB]
where
JO015 IN CASE WHEN JO026 = '0' THEN 
(SELECT RE002 FROM APSRES WHERE RE001 = 'liyu')
ELSE
(SELECT DISTINCT RE002 FROM APSRES )
END



------解决方案--------------------
SQL code


select 
    case when JO015 IN (SELECT RE002 FROM APSRES WHERE RE001 = 'liyu') then '0'
else '1' end as JO026,* from [dbo].[APSJOB]

------解决方案--------------------
SQL code

select JO026,* 
FROM 
    [dbo].[APSJOB]
where
    (isnull(JO026,'0') = '0' and JO015 IN (SELECT RE002 FROM APSRES WHERE RE001 = 'liyu'))
    or JO026='1'