选择ID之间的平均评级

时间:2016-04-22 12:21:57

标签: mysql sql average

我有两张桌子:

表“biz”和表“biz_rating”

BIZ

  • biz_id(int)
  • name(varchar)

biz_rating

  • biz_rating_id(int)
  • biz_id(int)
  • rating(int)

我想要一个查询来选择所有商业行,其中平均评级在1-2或3-4之间,或者< 5或> 5(评分是从0到5的数字)。

我确实没有想法如何开始。

我现在有这样的事情:

SELECT
    biz_id
FROM
    biz
WHERE
    biz_id IN
    (SELECT biz_id FROM biz_rating WHERE AVG(rating) BETWEEN 0 AND 5)
ORDER BY
    biz_id ASC

但这根本不起作用。

2 个答案:

答案 0 :(得分:1)

如果您想要名称和id

,可以加入表格
 select biz_id,name,b2.avgrating
 from BIZ as B1,
( select biz_id as bid, avg(rating) as avgrating
 from biz_rating
 group by biz_id 
 having avg(rating) between 0 and 5) as B2

 where B1.biz_id=B2.bid

如果需要,也可以选择平均评分

答案 1 :(得分:0)

只需使用聚合。你根本不需要join

select br.biz_id
from biz_rating br
group by br.biz_id
having avg(rating) between 1 and 2;  -- or whatever limits you want