multiple left join mysql 1

multiple left join mysql

SELECT news.id, users.username, news.title, news.date, news.body, COUNT(comments.id)
FROM news
LEFT JOIN users
ON news.user_id = users.id
LEFT JOIN comments
ON comments.news_id = news.id
GROUP BY news.id

Here is what the above code is Doing:
1. We’re selecting the news.id, users.username, news.title, news.date, news.body, and COUNT(comments.id) columns.
2. We’re joining the users table on the news.user_id column.
3. We’re joining the comments table on the comments.news_id column.
4. We’re grouping the results by the news.id column.

Similar Posts