Best way Selecting comments on multiple posts
I want to create a activity stream just like Google+ or Facebook.
My Problem is im really unsure whats the best way to query comments on multiple posts.
The easiest way is just selecting the comments in a loop, but this is really not the way i want to go.
Another possibility is joining them together.
SELECT c. *, p. *
FROM posts p
LEFT JOIN comments c ON p.post_id = c.post_id
WHERE so开发者_运维知识库mething = something
Or selecting them separately.
SELECT * FROM posts WHERE something = something
SELECT * FROM comments WHERE post_id = 1 OR post_id = 2 OR post_id = 3
Is there a better way to do that?
I'm sorry for my bad english.
Regards
Try this:
SELECT * FROM comments
WHERE post_id IN (
SELECT post_id FROM posts WHERE <condition>
)
Or, if you know the post_id
s:
SELECT * FROM comments WHERE post_id IN (1, 2, 3)
精彩评论