日期:2014-05-19  浏览次数:20470 次

这句查询怎么写???
表1
名称             编号             分数
张三             001                 90
李四             002                 70
小五             003                 80
林三             004                 30
四要             005                 20
三哥             006                 60
....
....


表2
起编号         止编号
001                 003
006                 006
...
...

现想查询表1的编号在表2起始编号中的数据.
SQL怎么写??

------解决方案--------------------
select a.名称,b.编号,a.分数 from 表1 a right join 表2 b
on a.编号=b.起编号
------解决方案--------------------
declare @表1 table(名称 varchar(20),编号 varchar(20),分数 smallint)
insert into @表1
select '张三 ', '001 ', '90 ' union all
select '李四 ', '002 ', '70 ' union all
select '小五 ', '003 ', '80 ' union all
select '林三 ', '004 ', '30 ' union all
select '四要 ', '005 ', '20 ' union all
select '三哥 ', '006 ', '60 '

declare @表2 table(起编号 varchar(20),止编号 varchar(20))
insert into @表2 select '001 ', '003 ' union all select '006 ', '006 '

select a.* from @表1 a, @表2 b where a.编号 between b.起编号 and b.止编号
/* 结果
名称 编号 分数
-------------------- -------------------- ------
张三 001 90
李四 002 70
小五 003 80
三哥 006 60
*/
------解决方案--------------------
没那么麻烦吧

select * from 表1 where 编号 in (select 起编号 from 表2)
------解决方案--------------------
没有看懂楼主的意思,***********(好象有两种意思)
--------------------------------------------------
declare @bb table (name char(10),no int,point int)
insert @bb select '张三 ' , 001 , 90
union all select '李四 ' , 002 , 70
union all select '小五 ' , 003 , 80
union all select '林三 ' , 004 , 30
union all select '四要 ', 005 , 20
union all select '三哥 ' , 006 , 60
declare @cc table (start int,end1 int)
insert @cc select 001 , 003
union all select 006 , 006

--查询表1的编号在表2起始编号中的数据
select name,no,point from @bb b,@cc c where b.no=c.start
--查询表1的编号在表2起始编号到中止编号范围內的数据
select name,no,point from @bb as b where exists(select 1 from @cc as c where b.no <=end1 and b.no> =start)
-------------------------------结果1
张三
1 90 三哥
6 60
----------------------------------结果2
张三 1 90
李四 2 70
小五 3 80
三哥 6 60
------------------------------------------