当前位置: 代码迷 >> Sql Server >> 求解:查询话语
  详细解决方案

求解:查询话语

热度:61   发布时间:2016-04-27 11:02:53.0
求解:查询语句
表A,字段JE,ML字段类型decimal(14, 2),ML/JE=MLL

表A里的数据MLL
1
2
3
4
1
2
2
3
5
6
7
6
5
4
  
写个语句能查询2到5之间的数据,也能查询大于等于5的,也可以查询小于等于4的
自己写的只能查询到2到5之间的
select ML/JE AS MLL from A 
where ML/JE >= 2 --如果大于等于空
and ML/JE <= 5 --或者小于等于空,就查不出数据来了

-----------------------

select ML/JE AS MLL from A 
where ML/JE >= '' --如果大于等于空
and ML/JE <= 5 --或者小于等于空,就查不出数据来了

怎样能让它能多种方式查询、只是查询条件的值不同,既可以查2到5之间的数据,又能查询当ML/JE >= 空的时候,and ML/JE <= 5的数据
求高手ing........

------解决方案--------------------
是下面这样么?
SQL code
declare @A table(MIL varchar(10))insert into @Aselect 1 union allselect 2 union allselect 3 union allselect 4 union allselect 1 union allselect 2 union allselect 2 union allselect 3 union allselect 5 union allselect 6 union allselect 7 union allselect 6 union allselect 5 union allselect 4 union allselect ''--查询大于2且小于5,和小于4的记录select * from @Awhere MIL between 2 and 5union allselect * from @Awhere MIL<4--查询大于空 and 小于等于5的记录select * from @Awhere MIL>'' and MIL<=5/*(15 行受影响)MIL----------23422355412312230(17 行受影响)MIL----------123412235540(12 行受影响)*/
------解决方案--------------------
每一种情况单独写一个查询语句,然后union all。是不是你要的结果?
  相关解决方案