当前位置:首页 » 编程语言 » sql下一行数据
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql下一行数据

发布时间: 2023-02-03 14:15:37

1. 关于查询sql中数据上一条记录和下一条记录的sql语句......

可用row_number来解决。

如student表

id name create_date

1 张三 2015-07-01

2 李四 2015-06-01

3 王五 2015-08-01

4 赵六 2015-04-01


如,要查找张三的create_date前和后各一条数据。

withtas
(selectstudent.*,row_number()over(orderbycreate_date)rnfromstudent)
select*fromstudentwherern=(selectt.rn+1fromtwheret.name='张三')
unionall
select*fromstudentwherern=(selectt.rn-1fromtwheret.name='张三')

结果应是:

id name create_date

2 李四 2015-06-01

3 王五 2015-08-01

2. 关于SQL查找一个值的下一行

oracle写起来很简单,借用楼上写法:
select num from numtable
where rownum in
(select rownum+1 from num_table where num=2)

但根据楼主的思路,一般查询起来会很麻烦,
可以换一个角度思考,不一定需要如此查询.
在实际编程中经常会遇到需要查询 当前行的下一行.
比如你可以再添加一个标识字段,或通过其它字段找到需要的行,或使用游标,或者在程序里处理,等等,根据不同的情况一定能找到一种简单有效的方法.

以上,希望可以给楼主一个新的思路

3. sql 查询返回满足条件的行和下一行

oracle里可以用ROWNUM伪列,写了一个例子用作参考:
with t1 as (select rownum n,a,b,lead(rownum)over(order by rownum) ne from cc)
,t2 as (select n,a,b,ne from t1 where 5 in (a,b))
select n,a,b from t2
union
select n,a,b from t1 where n in (select ne from t2)

把a,b换成你的字段,cc换成你的表名就可以了,我自己用数据测试了下,没问题。

4. sql2000数据库中如何实现同一列的数据相减(即下一行数据减去上一行数据)

表结构? 测试数据?

按哪一列排序?

测试数据.
CREATE TABLE temp (
year INT,
salary INT
);

INSERT INTO temp VALUES(2000, 1000);
INSERT INTO temp VALUES(2001, 2000);
INSERT INTO temp VALUES(2002, 3000);
INSERT INTO temp VALUES(2003, 4000);

预期要求结果:
year salary
2000 1000
2001 1000
2002 1000
2003 10000

SELECT
year,
salary - ISNULL((SELECT TOP 1 salary FROM temp t2 WHERE t2.year < temp.year ORDER BY year DESC), 0) AS salary
FROM
temp;
go

year salary
----------- -----------
2000 1000
2001 1000
2002 1000
2003 1000

(4 行受影响)

5. SQL取下一行的值问题

数据库结果集获取当前行下一行的值:
有个报表要加新字段,经过一段数据过滤后得到结果集r1。
列名

现在要获取到这样的结果集r2:

字段c为当前行下一行的字段a值(后面结果需要去处最后一行)
脑子一直没转过来的我想了一个笨办法(sql写的少):

select t.*,(select a from r1 where rowid=t.rowid+1) c
from r1 t

这样是能够实现,可效率太慢了,可以卡死人

6. sql server数据库追加一列数据,此数据为每一行的下一行数据,该怎么写sql

你用lead over 函数,最后处理一下最后一行 ,类似于
select id,value,time,lead(time) over(order by time) as time2 from tbA order by time

7. 如何获取SQL查询当前数据上一条和下一条的记录

方法一:x0dx0a查询上一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误):x0dx0a1x0dx0aselect * from table_a where id = (select id from table_a where id < {$id} [and other_conditions] order by id desc limit 1) [and other_conditions];x0dx0a查询下一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误):x0dx0a1x0dx0aselect * from table_a where id = (select id from table_a where id > {$id} [and other_conditions] order by id asc limit 1) [and other_conditions];