有一表结构如下:
Begin: 数值型, 表示一个范围的开始
End: 数值型, 表示一个范围的结束
Value: 数值型, 表示在这个范围内的值注: 范围是指从某个数值到另一个数值之间. 如: 0到100, 说明不超过100的; 100到200, 说明超过100到200的部分; 200到0, 超过200的部分.
其中End为零, 表示无穷大.下面是记录:
Begin End Value
--------------------------
0 100 10
100 200 20
200 300 30
300 400 40
400 500 50
500 0问题:
用一条SQL语句查出在这个某个范围的数值Value.例如:
查出不超过100的Value;
查出超过100到200的Value;
查出超过500的Value;
Begin: 数值型, 表示一个范围的开始
End: 数值型, 表示一个范围的结束
Value: 数值型, 表示在这个范围内的值注: 范围是指从某个数值到另一个数值之间. 如: 0到100, 说明不超过100的; 100到200, 说明超过100到200的部分; 200到0, 超过200的部分.
其中End为零, 表示无穷大.下面是记录:
Begin End Value
--------------------------
0 100 10
100 200 20
200 300 30
300 400 40
400 500 50
500 0问题:
用一条SQL语句查出在这个某个范围的数值Value.例如:
查出不超过100的Value;
查出超过100到200的Value;
查出超过500的Value;
welect * from tbl where value between 100 and 200
2.select * from tbl where begin > 100 and end <=200
3.select * from tbl where begin > 500
select value from table where value between 100 and 200
这个值为50~~就得到0到100的Value 10~~
这个值为250~~就得到200到300的Value 30~~
这个值为500~~就得到400到500的Value 50~~
这个值为501~~就得到超过500的Value~~所以我想用一条SQL语句得到根据这个值而得到Value~~
(select Vb from tbl where begin > 100 and end <=200) as Vb,
(select Vc from tbl where begin > 500) as Vc ......
from ......
where......
字段begin->beinn,end->endn
================================
测试
=================================
create table testt
( beginn int,
endn int,
value int)
======插入数据====================
insert testt select 0,100,10
union select 100,200,20
union select 200,300,30
union select 300,400,40
union select 400,500,50
union select 500,0,88888
==========60000==========
数据6000
select value from testt where ((beginn <6000) and ( 6000 < case endn when 0 then power(2,30) else endn end))
结果
beginn endn value
----------- ----------- -----------
0 100 10
100 200 20
200 300 30
300 400 40
400 500 50
500 0 88888(所影响的行数为 6 行)value
-----------
88888(所影响的行数为 1 行)================数据 220========================
beginn endn value
----------- ----------- -----------
0 100 10
100 200 20
200 300 30
300 400 40
400 500 50
500 0 88888(所影响的行数为 6 行)value
-----------
30================数据 410 ===================
beginn endn value
----------- ----------- -----------
0 100 10
100 200 20
200 300 30
300 400 40
400 500 50
500 0 88888(所影响的行数为 6 行)value
-----------
50(所影响的行数为 1 行)(所影响的行数为 1 行)