本文介绍了如何仅通过一个SQL查询打印帖子和评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以通过一个sql查询打印(PHP)我的所有博客文章和相关注释?
Is it possible to print out (PHP) all my blog posts + associated comments via one sql query?
如果是,怎么办?
我正在朝这个方向思考:
I was thinking in this direction:
SELECT p.post_id, p.title, cment_body FROM posts p LEFT JOIN comments c ON c.parent_id = p.post_id但是,这并没有达到我的预期
But this didn't work out as I expected
推荐答案使用一个SQL查询不是很方便,因为您有1条帖子和多个注释. 将帖子详细信息添加到每个评论中(在组合查询中)是浪费资源.
Using one SQL query is not very convenient, since you have 1 post and multiple comments. Having the post details added to each comment (in a combined query) is a waste of resources.
获取帖子详细信息并使用帖子的post_id查找属于该帖子的评论要方便得多.
It is much more convenient to get the post details and use post_id of the post to find the comments belonging to the post.
更多推荐
如何仅通过一个SQL查询打印帖子和评论
发布评论