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

这句查询如何写?

2012-02-17 
这句查询怎么写???表1名称编号分数张三00190李四00270小五00380林三00430四要00520三哥00660........表2起

这句查询怎么写???
表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
张三
190三哥
660
----------------------------------结果2
张三 190
李四 270
小五 380
三哥 660
------------------------------------------

热点排行