Mysql 查詢當前資料上一條和下一條的記錄

2021-09-27 04:16:05 字數 581 閱讀 8667

獲取當前檔案上一條與下一條記錄的原理是上一條的sql語句,從news表裡按從大到小的順序選擇一條比當前id小的新聞,下一條的sql語句,從news表裡按從小到大的順序選擇一條比當前id大的新聞。

如果id是主鍵或者有索引,可以直接查詢:

方法1:

1.select * from table_a where id = (select id from table_a where id <  order by id desc limit 1);   

2.select * from table_a where id = (select id from table_a where id > order by id asc limit 1);

方法2:

1.select * from table_a where id = (select max(id) from table_a where id < );   

2.select * from table_a where id = (select min(id) from table_a where id > );

Mysql 查詢當前資料上一條和下一條的記錄

獲取當前檔案上一條與下一條記錄的原理是上一條的sql語句,從news表裡按從大到小的順序選擇一條比當前id小的新聞,下一條的sql語句,從news表裡按從小到大的順序選擇一條比當前id大的新聞。如果id是主鍵或者有索引,可以直接查詢 方法1 sql view plain copy 1.select ...

SQL查詢當前資料上一條和下一條的記錄

id是指當前資料news id引數 方法一 string presql select top 1 from news where news id id order by news id desc string nextsql select top 1 from news where news id ...

查詢資料的上一條和下一條

查詢當前資料的下一條 select from 表 where id 當前資料的id order by id asc limit1 查詢當前資料的上一條 select from 表 where id 當前資料的id order by id asc limit 1 查詢當前資料的下一條 select f...