I'm trying to join 3 tables - users, posts and comments to get value like post title, post author, comment title, comment author.
users (id, name)
posts (id, content, user_id)
comments (content, user_id, post_id)
Right now I have query:
SELECT posts.id AS POST_ID, posts.content AS Post, posts.user_id AS PostAuthorID,
users.name AS PostAuthorName,
comments.content AS comment, comments.user_id AS CommentsAuthorID
FROM posts
JOIN users on users.id = posts.user_id
JOIN comments ON users.id = comments.post_id;
My question is how to get author's name of each comment and how get the author of the post? Right now by doing JOIN users on users.id = posts.user_id
I have post author but how to deal with comment author name? When I'm trying to add another condition to last join - JOIN comments ON users.id = comments.post_id AND users.id = comments.user_id
I receive table with posts and comments created by the same user. Any help is welcome.