一数据表,包含名为ID的字段,求一SQL语句,查出的结果需要符合如下要求:
1.id >10 and id < 20
2.id <> 15
3.id > 100
4.id = 4
重要的是上面的条件不能进行拆分,比如不能将1和2拆分为:(id>10 and id < 15) or (id > 15 and id<20)
在下在这里先谢过大家了!

解决方案 »

  1.   

    不好意思,再详细说明一下:
    实际上我是要做一个权限设置问题,
    举例说明:
    表内数据有:
    id
    3
    4
    5
    11
    12
    15
    16
    101
    102
    用户设置了四个权限,每个权限程序将自动产生如下条件:
    1.id >10 and id < 20(结果集中包含id是11~19的数据)
    2.not id = 15(结果集中不包含id是15的数据)
    3.id > 100(结果集中包含id大于100的数据)
    4.id = 4(结果集中包含id是4的数据)希望结果如下:
    id
    4
    11
    12
    16
    101
    102
    重要的是上面的条件不能进行拆分,比如不能将1和2拆分为:(id>10 and id < 15) or (id > 15 and id<20)
    在下在这里先谢过大家了!
      

  2.   

    select * from t where id >10 and id < 20 
    union
    select * from t where id <>15
    union
    select * from t where id > 100 
    union
    select * from t where id = 4 哈哈 不知道啥结果!
      

  3.   

    select * from 
    (
    select * from t where id >10 and id < 20 
    union 
    select * from t where id > 100 
    union 
    select * from t where id = 4
    ) T1
    where T1.id <>15 
    应该可以得到结果 不过很诡异啊!
      

  4.   

    我不明白楼主为什么要限定不能在条件中用and or
    没有实际意义啊?
      

  5.   

    select * from t where id >10 and id < 20 and id <>15
    union 
    select * from t where id > 100 
    union 
    select * from t where id = 4不知道楼主为什么这样来设计权限分配算法,有点怪怪的!
      

  6.   

    不是不能用and or
    是不能把条件进拆分而后再进行重新组合------------
    已经得到结果:
    id<>15 and (id=4 or (id>10 and id<20) or (id>100))
      

  7.   

    建议采用union实现:select * from t where id >10 and id < 20
    union all
    select * from t where id <>15
    union all
    select * from t where id > 100
    union all
    select * from t where id = 4 
      

  8.   

    为什么<>15和后面其他条件的关系是and呢?
      

  9.   

    union 吧。。条件放一起肯定没数据  ID<20 and ID>100不可能有