上一个/下一个按钮 sql 查询

发布于 2024-12-25 02:56:07 字数 580 浏览 0 评论 0原文

我有一个包含以下列的表数据:
|-a-|-b-|-文本-|
|-1-|-1-|-文本-|
|-1-|-2-|-文本-|
|-1-|-3-|-文本-|
|-2-|-1-|-文本-|
|-2-|-2-|-文本-|
|-2-|-3-|-文本-|
|-2-|-4-|-文本-|

例如,我在第三条记录上($current - 粗体),我需要进行 sql 查询以移动到 (2, 1)。

我有如下查询:

$previous = "SELECT a, b FROM `table` WHERE a <= $current_a AND b < $current_b ORDER BY a DESC, b DESC LIMIT 1";
$next = "SELECT a, b FROM `table` WHERE a >= $current_a AND b > $current_b ORDER BY a ASC, b ASC LIMIT 1";

尽管接下来会转到 (2, 4)。

有什么想法吗?

I have a table data with following columns:

|-a-|-b-|-text-|

|-1-|-1-|-text-|

|-1-|-2-|-text-|

|-1-|-3-|-text-|

|-2-|-1-|-text-|

|-2-|-2-|-text-|

|-2-|-3-|-text-|

|-2-|-4-|-text-|

e.g. I am on 3rd record ($current - in bold), I need to make sql query to move to (2, 1).

I have the query as below:

$previous = "SELECT a, b FROM `table` WHERE a <= $current_a AND b < $current_b ORDER BY a DESC, b DESC LIMIT 1";
$next = "SELECT a, b FROM `table` WHERE a >= $current_a AND b > $current_b ORDER BY a ASC, b ASC LIMIT 1";

Though however next goes to (2, 4) instead.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南薇 2025-01-01 02:56:07

您需要一个条件来分别处理这两种情况;当 a 值相同和不同时:

where (a = @current_a and b > @current_b) or (a > @current_a)

您还需要前一项:

where (a = @current_a and b < @current_b) ro (a < @current_a)

You need a condition that handles the two cases separately; when the a values are the same, and when they are different:

where (a = @current_a and b > @current_b) or (a > @current_a)

You need that for the previous item also:

where (a = @current_a and b < @current_b) ro (a < @current_a)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文