MySQL选择相对于评论计数的帖子和评论计数

时间:2016-01-07 14:26:00

标签: mysql

我按照他们的评论计数尝试select个帖子

SELECT id,title
,(SELECT COUNT(*) FROM ci_comments WHERE (post_id = p.id)) AS comment_count
FROM ci_posts p
WHERE type='post' AND active=1
ORDER BY comment_count DESC , date_modified DESC LIMIT 6

这有效

但我需要过滤comment_count > 10

SELECT id,title
,(SELECT COUNT(*) FROM ci_comments WHERE (post_id = p.id)) AS comment_count
FROM ci_posts p
WHERE type='post' AND active=1 AND comment_count > 10 
ORDER BY comment_count DESC , date_modified DESC LIMIT 6

结果: Unknown column 'comment_count' in 'where clause'

那么解决方案是什么?

1 个答案:

答案 0 :(得分:2)

尝试使用...查询查询结果

SELECT id,title
,(SELECT COUNT(*) FROM ci_comments WHERE (post_id = p.id)) AS comment_count
FROM ci_posts p
WHERE type='post' AND active=1
having comment_count >10
ORDER BY comment_count DESC , date_modified DESC LIMIT 6