I have 4 tables:
posts [post_id post_title post_body post_date post_by post_accepted]
users [user_id user_name user_pw]
comments [comment_id comment_user comment_co comment_post comment_date]
categories [categorie_id categorie_name]
Each table contain a data all data are belong to table posts:
categories<-posts
posts<-users
posts<-comments<-users
I want to get posts with id post_id and get with it a comments and who post it and get the category name and id.
I tried but I get the post but not all comments or if the post does not have comment it will no appear.
This my SQL query:
SELECT COUNT(comment_id),comments.*,categories.*,users.*,posts.*
FROM posts
JOIN categories on (posts.post_id = categories.categorie_id)
JOIN users on (posts.post_by = users.user_id)
LEFT JOIN comments on (posts.post_id = comments.comment_post)
WHERE posts.post_id='34'
AND posts.post_accepted = '1' ;
Try add GROUP BY users.id to your query