
Untitled
By: a guest on
Jun 14th, 2012 | syntax:
None | size: 0.71 KB | hits: 12 | expires: Never
How to join three degrees in mySQL
SELECT posts.id, posts.text, users.id, users.name, comments.text AS commentText, commenters.id, commenters.name
FROM posts
JOIN users
ON posts.userId = users.id
LEFT JOIN comments
ON posts.id = comments.postId
LEFT JOIN users AS commenters
ON comments.userId = users.id
WHERE posts.id = @postId
SELECT posts.id, posts.text, users.id, users.name, comments.text AS commentText, commenters.id, commenters.name
FROM posts
JOIN users
ON posts.userId = users.id
LEFT JOIN comments
ON posts.id = comments.postId
LEFT JOIN users AS commenters
ON comments.userId = commenters.id