當前位置:首頁 » 編程語言 » sql查詢上一條記錄
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql查詢上一條記錄

發布時間: 2022-09-22 16:01:29

sql如何獲取上一條..下一條..首尾記錄...

獲得上一條的id
:select
max(id)as
id
from
[表]
where
id<"[你的要查的id]"
order
by
[.....]
獲得下一條的id
:select
min(id)as
id
from
[表]
where
id>"[你的要查的id]"
order
by
[.....]
很笨的辦法但是很直觀·
不知道你是什麼資料庫··根據不同的資料庫有很多不同的寫法··
比如
mysql
中的
limit
或者
mssql
中的
top
寫法多了去啦··呵呵··上面舉個例子罷了··希望對你有幫助

⑵ 在SQL中怎麼查詢一條記錄

查詢表記錄的語句一般就是select * from 【表名】 where 【條件】;任何資料庫入門的書上都有。
如果查不到那你先查看一下你的這個表是不是屬於你當前登錄資料庫的用戶的(以oracle為例):
select * from user_tables where table_name='A' 如果沒有結果,那即使你用上述select * from A where ID=Y;也查不到的。

⑶ 關於查詢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

⑷ 請教一段能同時查出上一條記錄及下一條記錄的SQL

如果有主鍵自增或者按時間排序的資料庫記錄的話
直接根據當前的記錄的主鍵或時間,大概可以這樣寫
(select * from table where id > 當前id order by id asc limit 1 ) union
(select * from table where id < 當前id order by id desc limit 1)
這樣差不多能滿足

⑸ 關於查詢sql中數據上一條記錄和下一條記錄的sql語句......

sql中是設置的自增列啊;不需輸入當前的id,這里默認的是1;要是是1的話,就只能查到第2個,但3個就不曉得了回答: 當你查到第2個的時候,就獲取第二條的ID,點下一條的時候,就用第二條的id去查,後邊的類推~追問: 主要就是現在的id的獲取問題回答: 第一條就以0開始就行,後邊就獲取查出來的數據的id就行了, 你數據都查出來了,還獲取不到id?追問: id = Request.QueryString["id"]; 我是用這個獲取的;不知對否回答: 你能獲取到值就可以了,調試一下就行了~追問: 關鍵就是無法獲取到值..問題就出現在這里了,卡起了回答:

⑹ 如何獲取SQL查詢當前數據上一條和下一條的記錄

方法一:
查詢上一條記錄的SQL語句(如果有其他的查詢條件記得加上other_conditions以免出現不必要的錯誤):
1
select * 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];
查詢下一條記錄的SQL語句(如果有其他的查詢條件記得加上other_conditions以免出現不必要的錯誤):
1
select * 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];

⑺ sql如何查詢表的第一條記錄和最後一條記錄

第一條: select * from 表名 limit 1;(默認是升序排序,取第一條)
最後一條:select * from表名 order by 表_id desc limit 1(降序排序取第一條)

⑻ sql如何查詢表的第一條記錄和最後一條記錄

第一條: select * from 表名 limit 1;(默認是升序排序,取第一條)
最後一條:select * from表名 order by 表_id desc limit 1(降序排序取第一條)

⑼ SQL如何獲取上一條..下一條..首尾記錄...

獲得上一條的id :select max(id)as id from [表] where id<"[你的要查的id]" order by [.....]

獲得下一條的id :select min(id)as id from [表] where id>"[你的要查的id]" order by [.....]

很笨的辦法但是很直觀·
不知道你是什麼資料庫··根據不同的資料庫有很多不同的寫法··
比如 mysql 中的 limit 或者 mssql 中的 top
寫法多了去啦··呵呵··上面舉個例子罷了··希望對你有幫助

⑽ sql 查找上一條記錄中的值

SELECT 金額 FROM mjs WHERE id IN (SELECT
CASE
WHEN SIGN(id - 3) > 0
THEN MIN(id) WHEN SIGN(id - 3) < 0
THEN MAX(id) END AS id
FROM mjs WHERE id <> 3 GROUP BY SIGN(id - 3) ORDER BY SIGN(id - 3)) ORDER BY id ASC;