一道简单的SQL查询
我是一个初学者,我想问一下,要一个表只显示M行到N行的数据(M<N)怎么显示? 问题点数:20、回复次数:9Top
1 楼happyflystone(无枪的狙击手)回复于 2006-03-02 21:37:20 得分 15
select top (n -m) *
from (select top n * from table order by id desc) a
order by id ascTop
2 楼lxzm1001(*~悠悠蓝星梦~*)回复于 2006-03-02 21:40:48 得分 5
select top (n -m) *
from (select top n * from table) a
order by id descTop
3 楼cdq2104(蓝海)回复于 2006-03-02 22:23:06 得分 0
我按你们给我发的帖子做的
select top (8 -2) *
from (select top 8 * from shunxu order by id desc) a
order by id asc
可系统提示为什么是
服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: '(' 附近有语法错误。
服务器: 消息 156,级别 15,状态 1,行 2
在关键字 'order' 附近有语法错误。
Top
4 楼happyflystone(无枪的狙击手)回复于 2006-03-02 22:41:52 得分 0
select top (8 - 2) *
from (select top 8 * from shunxu order by id ) a
order by id descTop
5 楼happyflystone(无枪的狙击手)回复于 2006-03-02 22:57:00 得分 0
错了,top 后不支持计算
create table tb(id int)
go
declare @m int
declare @n int
set @m = 8
set @n = 6
set @n = @m - @n
declare @i int
set @i = 1
while @I <20
begin
insert tb select @i
select @i = @i + 1
end
select * from tb
exec('select top ' +@n+' *
from (select top '+@m+' * from tb order by id ) a
order by id desc')
drop table tb
/*
id
-----------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(所影响的行数为 19 行)
id
-----------
8
7
*/Top
6 楼lxzm1001(*~悠悠蓝星梦~*)回复于 2006-03-03 01:06:16 得分 0
select top 6 *
from (select top 8 * from table) a
order by id desc
Top
7 楼zjdyzwx(十一月猪)回复于 2006-03-03 10:37:41 得分 0
select top n * from (select top 999999 * from (select top M from table order by id asc) a order by a.id desc ) bTop
8 楼cdq2104(蓝海)回复于 2006-03-03 10:44:10 得分 0
我谢谢两位大哥哥对我的帮助。
由于我们寝晚上11点熄灯,所以没有及时看到你们的回帖,对此我表示道歉,希望你们谅解。
我又按 happyflystone(没枪的狙击手) 大哥做的修改了一下
例:从表‘tb’查第6行到第8行的数据
select * from(
select top 3 * -----(8-6+1)
from (select top 8 * from tb) a
order by id desc) b order by id
--------------------------------------------------
id
-----------
6
7
8
我想用存储过程写一下回更好些
但写到这里就不知怎么去写了
create procedure ord
@m int ,@n int
as
declare @Sub int
set @Sub=@n-@m+1
--select * from(
exec('select * from(
select top '+@Sub+' *
from (select top '+@n+' * from tb) a
order by id desc) b order by id ')
exec ord @m=6,@n=8
系统提示我
服务器: 消息 208,级别 16,状态 6,过程 ord,行 11
对象名 'ord' 无效。
我希望各位大哥再帮我一下,小弟不胜感激
Top
9 楼fengyaner(风颜儿)回复于 2006-03-03 13:31:07 得分 0
exec ord 6,8Top




