简体   繁体   English

mysql,通过分页获取树评论(邻接列表)

[英]mysql, get tree comments with pagination (adjacency list)

I have read a lot of information about mysql tree adjacency list我已经阅读了很多关于mysql树邻接列表的信息

I want to display the 10 most recent comments and ALL replies to this comment ( or where 10 comments where parentId is null and level = 1 but take all comments where exists parentId and level = 2 )我想显示最近的 10 条评论和对此评论的所有回复(或者 10 条评论where parentId is null and level = 1 ,但在exists parentId and level = 2的地方获取所有评论)

my table with data:我的数据表:

mysql comments table mysql评论表

You can use Common Table Expressions .您可以使用公用表表达式 Similar to the following code :类似于以下代码:

WHIT 
    CTE1 AS (SELECT * FROM comments WHERE parentId is null and level = 1)
SELECT * FROM CTE1 
UNION ALL
SELECT * FROM Comments WHERE parentId IN (SELECT id FROM CTE1) AND level = 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM