如何仅通过一个SQL查询打印帖子和评论 [英] How to print posts and comments with only one sql query

查看:101
本文介绍了如何仅通过一个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, c.comment_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查询打印帖子和评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆