獲取當前文件上一條與下一條記錄的原理是上一條的sql語句,從news表里按從大到小的順序選擇一條比當前ID小的新聞,下一條的sql語句,從news表里按從小到大的順序選擇一條比當前ID大的新聞。
如果ID是主鍵或者有索引,可以直接查找:
方法1:
- 1.select * from table_a where id = (select id from table_a where id < {$id} order by id desc limit 1);
- 2.select * from table_a where id = (select id from table_a where id > {$id} order by id asc limit 1);
方法2:
- 1.select * from table_a where id = (select max(id) from table_a where id < {$id});
- 2.select * from table_a where id = (select min(id) from table_a where id > {$id});
