當前位置:首頁 » 編程語言 » 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];