且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获取 MySQL 中特定行的偏移量?

更新时间:2023-02-26 12:35:38

往上走:

SELECT * FROM table WHERE id > 'your_current_id' ORDER BY id LIMIT 1;

下去:

SELECT * FROM table WHERE id < 'your_current_id' ORDER BY id DESC LIMIT 1;

ps: LIMIT 2 比较好,这样可以在只返回一条记录的情况下看到自己是在数据库中的第一条记录还是最后一条记录.

ps: it is better to make LIMIT 2, so that you can see that you are at the first or at the last records in the database when only one record is returned.