我应该使用哪个连接?

发布于 2024-12-04 05:19:32 字数 163 浏览 1 评论 0原文

假设我说一个名为:post 的表。另一张表是评论。

帖子和评论之间的关系是哪个评论属于一个帖子。而且一篇文章有​​很多评论。

那么,我想连接这两个表,我应该使用哪个连接?

Post
id

Comments
id
post_id

Let say I say a table called:post. Another table is comments.

And the relationship between post and comments is which comment belong to one post. And one post have many comments.

So, I want to join that two table, which join should I use??

Post
id

Comments
id
post_id

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七婞 2024-12-11 05:19:32

这取决于您到底想要做什么,以及您的数据库模式有哪些外键。

一般来说,对于 1 对 N 的关系(1 个帖子有 N 个评论),您可以对帖子和评论进行 LEFT OUTER JOIN,例如:

SELECT ...
FROM post p
LEFT OUTER JOIN comments c ON p.id = c.post_id

It depends on what exactly you want to do, and what foreign keys your database schema has.

Generally, for a 1-to-N relationship (1 post has N comments) you do a LEFT OUTER JOIN of posts to comments, for example:

SELECT ...
FROM post p
LEFT OUTER JOIN comments c ON p.id = c.post_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文