如何仅通过一个SQL查询打印帖子和评论

编程入门 行业动态 更新时间:2024-10-27 12:32:12
本文介绍了如何仅通过一个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查询打印帖子和评论

本文发布于:2023-10-23 08:19:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1520294.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:帖子   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!