简体   繁体   English

我应该使用哪个连接?

[英]Which join I should use?

Let say I say a table called:post. 假设我说了一个名为: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

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: 通常,对于一对一关系(1个帖子有N条评论),您需要在LEFT OUTER JOIN帖子中添加评论,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM