题
我的,我通过使用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
)的索引,第一次查询将花费几乎没有。
不隶属于 StackOverflow