首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

依据另一个表限定的范围查找记录,有实例

2012-12-25 
根据另一个表限定的范围查找记录,有实例。本帖最后由 i2428183811 于 2012-12-07 16:40:47 编辑create tabl

根据另一个表限定的范围查找记录,有实例。
本帖最后由 i2428183811 于 2012-12-07 16:40:47 编辑


create table t1(item varchar(15))
insert t1 select '111'
insert t1 select '222'
insert t1 select '333'
insert t1 select '444'
insert t1 select '555'
insert t1 select '666'
insert t1 select '777'
insert t1 select '888'
insert t1 select '999'
select * from t1

create table t2(c1 varchar(15),c2 varchar(15))
insert t2 select '111','222'
insert t2 select '333','666'
select * from t2

-- 查出t1中满足t2条件的记录,
-- 即(item>'111' and item<='222') or (item>'333' and item<='666')
-- 注:t2表中的范围记录数是不确定的,每条记录都是一小一大组成一个限制范围


drop table t1
drop table t2

[最优解释]

create table t1(item varchar(15))
insert t1 select '111'
insert t1 select '222'
insert t1 select '333'
insert t1 select '444'
insert t1 select '555'
insert t1 select '666'
insert t1 select '777'
insert t1 select '888'
insert t1 select '999'
select * from t1
 
create table t2(c1 varchar(15),c2 varchar(15))
insert t2 select '111','222'
insert t2 select '333','666'
select * from t2

select * from t1 a,t2 b where a.item between b.c1 and b.c2

/*
item            c1              c2
--------------- --------------- ---------------
111             111             222
222             111             222
333             333             666
444             333             666
555             333             666
666             333             666

(6 行受影响)


*/

[其他解释]

select * into #aa from t2 with(nolock)
declare @c1 varchar(15)
declare @c2 varchar(15)
declare @sqlStr varchar(4000)
set @sqlStr=''
while exists (select top(1)1 from #aa with(nolock))
begin
  select top(1) @c1=c1,@c2=c2 from t2 with(nolock)
  set @sqlStr=@sqlStr+'select * from t1 where item'>@c1 +'and item'<=@c2+' union all'


  delete #aa where c1=@c1 and c2=@c2
end
set @sqlStr=substring(@sqlStr,1,len(@sqlStr)-9)
exec(@sqlStr)


[其他解释]
select a.* from t1 a,t2 b where a.item > b.c1 and a.item<=b.c2
[其他解释]
已经出来了!~~~~~~~~~~~~~
[其他解释]

select * into #aa from t2 with(nolock)
declare @c1 varchar(15)
declare @c2 varchar(15)
declare @sqlStr varchar(4000)
set @sqlStr=''
while exists (select top(1)1 from #aa with(nolock))
begin
  select top(1) @c1=c1,@c2=c2 from #aa with(nolock)
  set @sqlStr=@sqlStr+' select * from t1 where item>'+''''+@c1+''''+' and item<='+''''+@c2+''''+' union all'
  delete #aa where c1=@c1 and c2=@c2
end
set @sqlStr=substring(@sqlStr,1,len(@sqlStr)-9)
select @sqlStr
exec(@sqlStr)

--结果

----------------------------------------------------------------------------------------------------------------
 select * from t1 where item>'111' and item<='222' union all select * from t1 where item>'333' and item<='666' 

(1 行受影响)

item
---------------
222
444
555
666

(4 行受影响)

[其他解释]
学习来了,,
[其他解释]
引用:
SQL code
?



12345678910111213141516171819202122232425262728293031323334

create table t1(item varchar(15)) insert t1 select '111'insert t1 select '222'insert t1 select '333'insert t1 select '……

学习了,我刚开始还以为要用动态拼接查询语句呢。

热点排行
Bad Request.