如果ID是主键或者有索引,可以直接查找:
方法一:
查询上一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误):
select*fromtable_a
whereid=
(selectidfrom
table_awhereid
orderbyiddesclimit1
)
[andother_conditions];
查询下一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误):
select*fromtable_a
whereid=
(selectidfromtable_a
whereid>{$id}[andother_conditions]
orderbyidasclimit1
)
[andother_conditions];
方法二:
查询上一条记录的SQL语句((如果有其他的查询条件记得加上other_conditions以免出现不必要的错误))
select*fromtable_a
whereid=
(selectmax(id)fromtable_a
whereid
)
[andother_conditions];
查询下一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误):
select*fromtable_a
whereid=
(selectmin(id)fromtable_a
whereid>{$id}[andother_conditions]
)
[andother_conditions];
如何在MySQL中查询当前数据上一条和下一条的记录
标签:order如何sqlminclassconditionmysq语句错误
本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉
本文系统来源:/lqylqy/p/6714059.html