Sql语句
SELECT
a.*
FROM
lf_comment AS a,
(SELECT
GROUP_CONCAT(id order by id desc) AS ids
FROM
lf_comment
GROUP BY root_id) AS b
WHERE
FIND_IN_SET(a.id, b.ids) BETWEEN 1 AND 2
ORDER BY a.root_id ASC, a.id DESC;
SQL语句解析
GROUP_CONCAT得到每组用逗号分隔的列值,最大长度可以通过group_concat_max_len环境变量设置。
find_in_set返回id在ids中的位置,不存在返回0。