表格上的字段名称
对于名称“name1”的给定值,给我那行以及之前的N行和之后的N行(按字母顺序排列)
答案 0 :(得分:1)
在两个select语句中是否用你想要的N替换了数字5并更改了表名,这样就可以了。同样用正确的列名替换星号。如果您对此有任何问题,请告诉我。
select * from
(
Select *
,row_number() over (order by firstname desc) as 'rowNumber'
from attendees
) as temp
where rowNumber between
(
select rownumber-1
from
(
Select *, row_number() over (order by firstname desc) as 'rowNumber'
from attendees
) as temp
where firstname = 'name1') AND (
select rownumber+1
from
(
Select *, row_number() over (order by firstname desc) as 'rowNumber'
from attendees
) as temp
where firstname = 'name1')
答案 1 :(得分:0)
以下内容为您提供name ='name4'的行,此前的两行以及之后的两行。
drop table t;
create table t(
name varchar(20)
,primary key(name)
);
insert into t(name) values('name1');
insert into t(name) values('name2');
insert into t(name) values('name3');
insert into t(name) values('name4');
insert into t(name) values('name5');
insert into t(name) values('name6');
insert into t(name) values('name7');
commit;
(select name from t where name = 'name4')
union all
(select name from t where name > 'name4' order by name asc limit 2)
union all
(select name from t where name < 'name4' order by name desc limit 2);
+-------+
| name |
+-------+
| name1 |
| name2 |
| name4 |
| name5 |
| name6 |
+-------+
编辑: 按照cyberkiwi的指示增加了降序(否则我会在错误的一端得到“第一”2项)。