我使用的是sybase数据库。
我必须从我的表selfjoin(id,salary);
中选择每第n行
我使用
select top 1 * from (select top 4 * from selfjoin order by id desc) order by id
不过,我得到了一个错误。
An ORDER BY clause is not allowed in a derived table.
下面的sql也会导致错误
select id from selfjoin order by id asc limit 2
--error :-`Incorrect syntax near 'limit'`
下面的sql还抛出了一个错误。
SELECT ROW_NUMBER() OVER (ORDER BY id ASC) AS rownumber,salary from selfjoin;
--error :- `Incorrect syntax near the keyword 'OVER'.`
我也读取了this link,但没有查询有效。我也检查了this页面,但没有得到正确的结果。
问题的变化:-表中的薪水是升序的。即根据工资的升序找到第n行。
发布于 2013-02-07 09:56:46
请查看以下查询:
SELECT * from select count where (n-1) =(select count(id) from selfjoin s2 where s1.id>s2.id)
其中n是行号
发布于 2013-02-07 13:34:22
好吧,如果id是某个连续递增的数字,那么你可以这样做:-
create table #tmp2(id numeric identity,name char(9))
insert into #tmp2 values("B")
insert into #tmp2 values("C")
insert into #tmp2 values("D")
insert into #tmp2 values("E")
insert into #tmp2 values("F")
insert into #tmp2 values("G")
insert into #tmp2 values("H")
insert into #tmp2 values("I")
insert into #tmp2 values("J")
insert into #tmp2 values("K")
insert into #tmp2 values("L")
select t1.* from #tmp2 t1,#tmp2 t2
where t1.id=t2.id*2 ---(nth number)
或者,如果id不是从1开始,则
select t1.* from #tmp2 t1,#tmp2 t2
where t1.id=((t1.id+1)-t2.id)*2 ---(nth number)
结果:
id名称
2 C
4 E
6 G
8 i
10K
https://stackoverflow.com/questions/14743821
复制相似问题