[英]Show table results based on count in sub query
This is really doing my head in, it be a simple query I thought. 这确实是我的脑筋,我想这是一个简单的查询。
blog table: 博客表:
blog_id
blog_name
blog_copy
comments table: 评论表:
comment_id
comment_copy
comment_by
blog_id
I want to show blog items where there are more than 3 comments and also order them by the volume of replies. 我想显示博客项目中有3条以上的评论,并按回复数量对其进行排序。
I tried many queries including this but it just doesn't work: 我尝试了很多查询,包括此操作,但它不起作用:
SELECT
*, blog_id as BID ,
(SELECT blog_id
FROM comments
WHERE blog = BID HAVING COUNT(*) > 3) AS t2
FROM blog
WHERE mostcomments > '3'
ORDER by mostcomments ASC
It says that mostcomments
doesn't exist. 它说
mostcomments
不存在。 I've done it other ways and it executes but counts comments on totals overall not per blog_id it's looking up 我已经用其他方法完成了它,但它执行了,但是对总数的评论计数不是按它正在查找的blog_id进行的
You need to use GROUP BY
: 您需要使用
GROUP BY
:
select b.blog_id, count(*)
from blog b
join comments c on b.blog_id = c.blog_id
group by b.blog_id
having count(*) > 2
order by count(*) desc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.