用于平均和舍入的SQL查询

时间:2011-11-14 10:26:48

标签: php mysql sql math

我需要一个查询,它会更新Games中的一行,使其记录的Ratings的平均值乘以它所拥有的Plays的数量。这是我到目前为止所做的,但它似乎没有起作用:

UPDATE Games
SET PlaysRating = ROUND(AVG
                       (SELECT `Rating` FROM GameRatings WHERE GameID = '37') 
                     * (SELECT COUNT(*) FROM Plays WHERE GameID = '37')) 
WHERE ID = 37

1 个答案:

答案 0 :(得分:3)

看起来像这样:

UPDATE Games
SET    PlaysRating = round(
       (SELECT count(*)    FROM Plays       WHERE GameID = 37)
     * (SELECT avg(Rating) FROM GameRatings WHERE GameID = 37))
WHERE  ID = 37

BTW,假设ID是数字类型,我删除了引号''