在mysql查询中获取记录的偏移量 [英] getting the offset of a record in mysql query
本文介绍了在mysql查询中获取记录的偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对一个表进行了一些查询,该表可以按几列中的任何一列进行排序,以及结果集中一行的 id.如何获取下一条或上一条记录(或完整结果集中的 id 偏移量).
I have some query on a table which can be sorted by any of several columns, and an id of a row in the resultset. How can I get the next or previous records (or the offset of the id in the complete resultset).
即.假设我有下表
ID First_Col Second_Col Name
1 34 24 John
2 40 22 James
3 48 5 Hugh
如果按 first_col 排序,并且给定 id 2,则下一条记录将为 3,但如果按 Second_Col 排序,则下一条记录将为 1.
If sorting by first_col, and given id 2, the next record would be 3, but if sorting by Second_Col, the next record would be 1.
给定 sortBy 表达式(传递给我的函数)和 id,我如何轻松找到下一条记录?
Given the sortBy expression (which is passed to my function), and the id, how can I find the next record easily?
推荐答案
这应该有效:
SELECT
*
FROM
names
WHERE
Second_Col > (SELECT Second_Col FROM names WHERE ID = 2)
ORDER BY
Second_Col
LIMIT 1
参见 http://sqlfiddle.com/#!2/fac71/3一个>
这篇关于在mysql查询中获取记录的偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文