php - 文章评论嵌套显示mysql表结构如何设计(形式如网易新闻评论)
阿神
阿神 2017-04-10 17:42:11

现在有一张post表 (结构不能改变)
tid 主题pid
pid 回复id

现在新增了评论嵌套显示功能,即tid=1
a评论了一条 pid为1
b回复了a的评论 pid为2
c回复了b的评论 pid为3
d回复了a pid为4

显示为
1楼 pid1
2楼 pid1,pid2
3楼 pid1,pid2,pid3
4楼 pid1,pid4
即每一个对他人的回复都需要引用之前所有的回复。
(形式跟网易新闻的评论一样)
我目前想到一种结构就是post_conversation pid ,to_pid
这样 2楼回复就插入 2,1

 3楼回复就插入两条     3,1
                     3,2
 4楼回复就插入         4,1          

如果有一个99楼的对话 就得插入98条数据

这样有个好处就是可以很方便取出任意一个pid的评论情况
select topid from post_conversation  where pid = 'xxx' order by pid ;
但是会造成多的重复数据  一个99楼对话得插入1+2+3+。。。+99条

如果这么设计 post_conversation
pid,parent_pid(被回复的pid)
数据量是小了,(每次回复只插入一条);
但是查询每一个pid的所有对话,很麻烦(我想到的是递归查询数据库);

select parent_pid from post_conversation  where pid = 'xxx'
    select parent_pid from post_conversation  where pid = 'parent_pid '
        ...
        一直到parent_pid 为0为止,查询出所有的对话pid

请问大家有没有好的建议

阿神
阿神

闭关修行中......

reply all(2)
Peter_Zhu

谢邀
曾经我这边也是用最原始那种递归查询的方法来实现(菜单,评论)无限嵌套。后来在 Laravel-China 社区看到管理员发的一篇文章,文章地址https://laravel-china.org/topics/2124,使用 预排序遍历树算法(Nested set model) 实现无限树状层级模型(标签系统,菜单系统,评论系统等)。题主可以参考下。

Popular topics
More>
Latest downloads
More>
web effects
Website source code
Website materials
Front end template
About us Disclaimer Sitemap
PHP Chinese website:Public welfare online PHP training,Help PHP learners grow quickly!