日期:2014-05-16  浏览次数:20482 次

如何值在那个范围列?
id  col_min   col_max  
1   1                6
2   12              18
3    100            200
4    1010            2000


现在有一个150  ,怎样确定是 150   对应的范围记录是id=3的记录?
------解决方案--------------------

use tempdb
if OBJECT_ID('test') is not null
drop table test
create table test
(
id int null,
col_min int null,
col_max  int null
)

insert into test values(1,1,6),
(2,12,18),
(3,100,200),
(4,1010,2000)

select id from test
where col_max>150 and col_min<150
--结果
--id
--3


这样可以吗?
------解决方案--------------------
这样吗:

select *
from tb
where 150 between col_min and col_max

------解决方案--------------------

create table s1
(id int,col_min int,col_max int)

insert into s1
 select 1,1,6 union all
 select 2,12,18 union all
 select 3,100,200 union all
 select 4,1010,2000

create table s2
(x int)

insert into s2
 select 150 union all
 select 16 union all
 select 1500


select x,
      (select top 1 id 
       from s1
       where s2.x between s1.col_min and s1.col_max)'id' 
 from s2

/*
x           id
----------- -----------
150         3
16          2
1500        4

(3 row(s) affected)
*/