求助1个SQL的写法

[复制链接]
查看11 | 回复2 | 2007-7-19 08:57:15 | 显示全部楼层 |阅读模式
select * from t
id
----------
1
2
3
4
5
6
7
8
9
10
我想select * from t where id =3 的时候返回
id
----------
1
2
3
select * from t where id =4
id
----------
2
3
4
除了返回自己的时候 还返回前3行数据有什么办法?


回复

使用道具 举报

千问 | 2007-7-19 08:57:15 | 显示全部楼层
select *
from (select level lv from dual connect by level 复制代码如果可以改表结构的话,可以这样:alter table t_source add rn number;
update t_source set rn=rownum;
create index i_rn on t_source(rn);
select * from t_source t where rn between &rn - 2 and &rn;复制代码
回复

使用道具 举报

千问 | 2007-7-19 08:57:15 | 显示全部楼层
要在查询时有序,
或者有索引,或者全表扫描+排序
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行