MySQL Multiple Left Joins

前端 未结 2 1226
春和景丽
春和景丽 2020-12-13 01:31

I am trying to create a news page for a website I am working on. I decided that I want to use correct MySQL queries (meaning COUNT(id) and joins instead of more than one que

2条回答
  •  情书的邮戳
    2020-12-13 02:03

    To display the all details for each news post title ie. "news.id" which is the primary key, you need to use GROUP BY clause for "news.id"

    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
    

提交回复
热议问题