AVG / COUNT MySQL查询无效

时间:2014-10-10 08:27:45

标签: mysql

我对MySQL查询不是很有经验,所以我可能做错了。 简化我的查询是这样的:

SELECT item.*, AVG(itemRating.rating) as 'rating', COUNT(itemRating.rating) as 'ratingCount' 
  FROM item, itemRating 
 WHERE item.id IN (...) 
   AND itemRating.item_fk = item.id 
 GROUP BY itemRating.item_fk

除了项目没有评级(itemRating表中没有记录)外,它工作正常。有什么方法可以解决这个问题而不会丢失信息吗?

1 个答案:

答案 0 :(得分:1)

SELECT item.id, 
       AVG(itemRating.rating) as 'rating', 
       COUNT(itemRating.rating) as 'ratingCount' 
  FROM item
  LEFT JOIN itemRating ON itemRating.item_fk = item.id 
 WHERE item.id IN (...) 
 GROUP BY item.id