使用评论计数器对新闻进行一次查询

时间:2019-07-01 21:56:44

标签: sql

我有两个表:network_mode: hostnews

我需要为新闻内容和评论数写一个查询。

我尝试过

comments

我想应该有很多“ AS”。有人可以帮助我吗?谢谢

1 个答案:

答案 0 :(得分:0)

您缺少GROUP BY

SELECT n.id, n.title, n.date, n.author_name, n.short_content,
       COUNT(c.comment) as cnt
FROM php_base.news n LEFT JOIN
     php_base.comments c 
     ON n.id = c.news_id
GROUP BY n.id, n.title, n.date, n.author_name, n.short_content,
ORDER BY n.date DESC LIMIT 10