我有两张桌子:
salon_ranks
:
+-----------+-----------+-----------+-----------+
| salon_id |category_id| user_id | rank |
+-----------+-----------+-----------+-----------+
| 10 | 1 | 999 | 1 |
| 10 | 2 | 999 | 1 |
| 10 | 1 | 888 | 5 |
| 10 | 2 | 888 | 5 |
+-----------+-----------+-----------+-----------+
categories
:
+------+-----------+
| id | name |
+------+-----------+
| 1 | prices |
| 2 | service |
+------+-----------+
我正在尝试有效地获得如下所示的东西:
+-----------+-----------+-----------+
| salon_id |category_id| AVG(rank) |
+-----------+-----------+-----------+
| 10 | 1 | 3 |
| 10 | 2 | 3 |
+-----------+-----------+-----------+
对我来说最有意义的是这样的查询:
SELECT salon_id, category_id, AVG(rank)
FROM salon_ranks
INNER JOIN categories ON category_id = id
GROUP BY salon_id, category_id
但由于某种原因,我没有得到平均值,而是得到了这个:
+-----------+-----------+-----------+
| salon_id |category_id| AVG(rank) |
+-----------+-----------+-----------+
| 10 | 1 | 1 |
| 10 | 2 | 5 |
+-----------+-----------+-----------+
知道为什么会发生吗?什么可能是正确的查询?
对不起,是我的错。我从原始表格中获取的数据错误。真的很抱歉误导你:(