gpt4 book ai didi

sql - MySQL/PHP : keeping an extra record while paging

转载 作者:行者123 更新时间:2023-11-29 05:46:29 25 4
gpt4 key购买 nike

我有一个记录列表,我想使用 LIMIT 进行分页,但是没有 LIMIT 返回的第一条记录也是其余记录的根标识符,我需要为每一页保留它。这可能吗? (我只是不想运行额外的 sql 语句)

id  |  index  |  title
1 | 0 | index of titles
2 | 1 | title1
3 | 1 | title2
4 | 1 | title3
5 | 1 | title4

LIMIT 3、2 应该返回...

id  |  index  |  title
1 | 0 | index of titles
4 | 1 | title3
5 | 1 | title4

最佳答案

SELECT  *
FROM (
SELECT *
FROM mytable
WHERE index = 0
ORDER BY
index, id
LIMIT 1
) q
UNION ALL
(
SELECT *
FROM mytable
WHERE index = 1
ORDER BY
index, id
LIMIT 3, 2
) q2

如果你在 (index, id) 上有一个复合键(在 MyISAM 中)或者只是在 index 上有一个索引(在 InnoDB),第一次查询几乎不需要任何费用。

关于sql - MySQL/PHP : keeping an extra record while paging,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1226660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com