SQL条件AND子句?

问题描述:

在我的查询中,如果发生特定情况,我想在查询中添加和子句。 见我的查询的WHERE子句SQL条件AND子句?

1  and ps.dept=dept.deptid 

2  and ps.sdept=deptsub.deptid 

3  and 

4 if(ps.dept<>ps.sdept) 

5 begin 

6 deptsub.Parent=dept.deptid 

7 end 

8 and ps.deptcategory=deptcat.category 

在4我想,如果条件满足,然后6应该在查询其他以不这是怎么possible.thanks!

如何:

and ps.dept=dept.deptid 
and ps.sdept=deptsub.deptid 
and (ps.dept=ps.sdept or deptsub.Parent=dept.deptid) 
and ps.deptcategory=deptcat.category 

尝试更换线4,5,6,7本:

deptsub.Parent = 
case when ps.dept <> ps.sdept then dept.deptid 
else deptsub.Parent 
end 

当你的条件满足的情况下声明将取代dept.deptid,否则将只是替补deptsub.parent - 这将总是= deptsub.parent

and ps.dept=dept.deptid 
and ps.sdept=deptsub.deptid 
and 
(
    ((ps.dept<>ps.sdept) and (deptsub.Parent=dept.deptid)) 
    or 
    (ps.dept = ps.sdept) 
) 
and ps.deptcategory=deptcat.category