2

我有一个类似这样的 MYSQL 表:

Fruit   |Color  |Brand  |Rating
-------------------------------
Apple   Red     b1      5.4
Banana  Yellow  b2      1.1
Apple   Red     b1      2.2
Apple   Green   b3      4.1
Apple   Green   b2      1.9
Apple   Red     b1      3.7

哪个是检索每个不同项目的平均评分的正确查询?ie 如何获得组合“Apple Red b1”的平均评分?

预先感谢您的帮助。

4

2 回答 2

3

尝试-

SELECT Fruit, Color,Brand, AVG(Rating)
FROM Fruit_Ratings
GROUP BY Fruit, Color, Brand;

来自 - http://dev.mysql.com/doc/refman/5.5/en/group-by-functions.html#function_avg

SQLFiddle 显示查询 - http://sqlfiddle.com/#!2/c24aa/1

于 2012-12-12T19:21:04.357 回答
0

这应该适用于您的情况:

SELECT AVG(Rating) FROM table GROUP BY Fruit, Color, Brand;
于 2012-12-12T19:23:14.043 回答