表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........

解决方案 »

  1.   

    ML/JE的结果是数字,你和一个字符串比较,肯定会有问题。
    我不知道你>= 空代表什么, 空=0?不管如何,多条件查询,可以使用or联接,代码如下:
    select ML/JE AS MLL from A  
    where (ML/JE >= 2 and ML/JE <= 5)
    or(ML/JE<0)
    or(...)
      

  2.   

    是下面这样么?declare @A table(MIL varchar(10))
    insert into @A
    select 1 union all
    select 2 union all
    select 3 union all
    select 4 union all
    select 1 union all
    select 2 union all
    select 2 union all
    select 3 union all
    select 5 union all
    select 6 union all
    select 7 union all
    select 6 union all
    select 5 union all
    select 4 union all
    select ''
    --查询大于2且小于5,和小于4的记录
    select * from @A
    where MIL between 2 and 5
    union all
    select * from @A
    where MIL<4
    --查询大于空 and 小于等于5的记录
    select * from @A
    where MIL>'' and MIL<=5
    /*
    (15 行受影响)
    MIL
    ----------
    2
    3
    4
    2
    2
    3
    5
    5
    4
    1
    2
    3
    1
    2
    2
    3
    0(17 行受影响)MIL
    ----------
    1
    2
    3
    4
    1
    2
    2
    3
    5
    5
    4
    0(12 行受影响)
    */
      

  3.   

    每一种情况单独写一个查询语句,然后union all。是不是你要的结果?
      

  4.   

    where MIL>'' and MIL<=5
    求解MIL>''怎么比较的啊