PHP MySQL double inner join

You're missing a key relationship in your model. You need to have a column in replies and comments for post_id, and then join the tables on post_id.

Once you've made this change, then your query would look like this:

SELECT c.*, r.* 
FROM posts p
INNER JOIN comments c ON p.id=c.post_id
INNER JOIN replies r ON p.id=r.post_id
WHERE p.user_id=$user_id

Try using left joins instead of inner joins. This will ensure that all rows from the POSTS table matching the WHERE criteria are present.

SELECT *
FROM POSTS A
LEFT JOIN COMMENTS B ON A.POST_ID=B.COMMENT_ID
LEFT JOIN REPLIES C ON A.POST_ID=C.REPLY_ID
WHERE A.USER_ID='X'

Tags:

Mysql

Php

Join