我正在使用sybase数据库
我必须从表selfjoin(id,salary);
我用
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行。
答案 0 :(得分:0)
查看以下查询: -
SELECT * from selfjoin s1其中(n-1)=(从selfjoin s2中选择count(id) 其中s1.id> s2.id)
其中n是rownumber
答案 1 :(得分:0)
好吧,如果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 name
2 C
4 E
6 G
8我
10 K