查询当前记录上一条与下一条记录的原理:
- 上一条的sql语句,从table表里按从大到小的顺序(正序ASC)选择一条比当前ID小的记录。
- 下一条的sql语句,从news表里按从小到大的顺序(倒序DESC)选择一条比当前ID大的新闻。
方式一:
上一条:
select * from table where id = (select id from table where id < {$id} order by id desc limit 1);
下一条:
select * from table where id = (select id from table where id > {$id} order by id asc limit 1);
方式二:
上一条:
select * from table where id = (select MAX( id) from table where id < {$id} );
下一条:
select * from table where id = (select MIN( id) from table where id > {$id} );
其中ID为表table的主键