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

请教多条件查询?
SQL code

select ww.*,qq.renshu from
(select a.*,b.mingcheng,c.mingma,d.mingcheng as mch,e.mingcheng as pl,f.DFMingCheng as zj 
from Tbl_checkIn a,tbl_diqu b,tbl_ShouPiao c,tbl_YanPiaoDian d,tbl_PiaoLei e,Tbl_SFZ f 
where a.diqu=b.bianhao and a.KaHao=c.kahao  
and a.CheckInNo=d.bianhao 
and left(a.zhengjian,6)=f.SFZBianHao 
and a.piaolei=e.bianhao 
and mingma>='11'  
and mingma<='22' 
and convert(char(10),CheckInTime,120) >= '20120615' 
and convert(char(10),CheckInTime,120) <= '20120625') ww
join (select zhengjian,sum(renshu) as renshu from tbl_checkin group by zhengjian) qq 
on ww.zhengjian =qq.zhengjian


请教各位老师,上面这条语句已经无法满足平时查询的要求,现在需要在增加五六个mingma的查询条件,直接and mingma不管用,应该如何写呢?谢谢!

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


--这种写法测试通过
select * from sys_code where 1=1 and ( ( id>'106' and id<'108') or  ( id>'112' and id<'14') ) and 1=1

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


--.....
SELECT *
FROM tb
WHERE (
 CASE WHEN mingma>='11' and f_ConsumerName<='22' THEN 1 ELSE 0 END +
 CASE WHEN mingma>='28' and mingma<='32' THEN 1 ELSE 0 END +
 CASE WHEN mingma>='57' and mingma<='75' THEN 1 ELSE 0 END +
 CASE WHEN mingma>='82' and mingma<='91' THEN 1 ELSE 0 END +
) >= 1

--组合条件搜索,当上述4种条件任意一种或多种同时满足