简体   繁体   中英

Which join I should use?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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