SQL - WHERE Condition on SUM()

前端 未结 4 492
长发绾君心
长发绾君心 2020-12-19 18:12

Is it possible to do something like this:

SELECT 
  `e`.*, 
  `rt`.`review_id`, 
  (SUM(vt.percent) / COUNT(vt.percent)) AS rating 
FROM `catalog_product_ent         


        
4条回答
  •  别那么骄傲
    2020-12-19 18:57

    Use HAVING for aggregate conditions (such as the one you have)

    SELECT `e`.*, `rt`.`review_id`, (SUM(vt.percent) / COUNT(vt.percent)) AS rating 
    FROM `catalog_product_entity` AS `e` 
    INNER JOIN `rating_option_vote` AS `vt`
        ON vt.review_id = e.review_id 
    WHERE ( rating >= '0') 
    GROUP BY `vt`.`review_id'
    HAVING (SUM(vt.percent) / COUNT(vt.percent)) > 0
    

提交回复
热议问题