SQL查询根据计数过滤掉结果

时间:2017-02-27 11:29:14

标签: mysql

我有下表hosts,列为:

http://registrylocalhostratingsrating_idprod_idrating [buyer是唯一的]

status列的值从prod_typerating_id,而rating只需要2个值15

我想编写一个SQL查询,检索所有那些平均评分来自buyer的买家的产品,以使特定产品的0买家数大于5。

例如,我希望结果如下:

1

我编写了以下查询,但它无效:

SELECT prod_id,avg(rating),count(1)WHERE buyer = 1 AND count(1)> `prod_id` `avg(rating)` `count(*)` ksks 3.2 6 kglgkj 4.0 8 jfkfj 2.0 13 GROUP BY prod_id;

我收到错误无效使用群组功能

任何帮助将不胜感激。感谢

2 个答案:

答案 0 :(得分:3)

您必须使用HAVING来检查COUNT。使用以下查询:

SELECT prod_id, AVG(rating), COUNT(*) 
WHERE buyer = 1 
GROUP BY prod_id
HAVING COUNT(*) > 5

答案 1 :(得分:1)

您可以使用HAVING:

SELECT prod_id, avg(rating), count(*) 
WHERE buyer = 1 
GROUP BY prod_id
HAVING count(*) > 5