我试图根据WHERE子句找到某些行的总和的平均数。这是我到目前为止所做的:
SELECT SUM(rating), id
FROM rating
WHERE id=$id;");
所以类似“行数之和”/“返回的行数”
答案 0 :(得分:4)
答案 1 :(得分:0)
不是那么简单:
SELECT SUM(rating) AS sum, AVG(rating) AS avg, id
FROM rating
WHERE id = $id
GROUP BY id;
答案 2 :(得分:0)
MySQL实际上有一个内置的平均功能
SELECT AVG(rating) as average_rating,
id
FROM rating
WHERE id=$id
GROUP BY id
如果由于某种原因你真的想使用SUM,你可以使用
SELECT SUM(rating)/COUNT(rating) as average_rating,
id
FROM rating
WHERE id=$id
GROUP BY id
希望不是!