日期:2014-05-16 浏览次数:20524 次
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
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)
*/