我使用下面的表格......MySQL的總和和組給予錯誤的結果
stats (id, game, user_id, rank, score, dnt)
teams(id, name, dnt)
users(id, username, email, team_id, dnt)
我要搶根據統計排名前10位的高得分的球隊(球隊總比分被其使用者對總成績)
爲統計樣本數據...
| id | game | user_id | rank | score | dnt |
+----+------+---------+-------+------+-----+
| 1 | test | 5 | 2 | 2200 |
+--------+----------+----------+-----+-----+
| 2 | test | 3 | 1 | 2500 |
+--------+----------+----------+-----+-----+
隊
| id | name | dnt |
+----+-------+-----+
| 1 | team1 | |
+----+-------+-----+
| 2 | team2 | |
+----+-------+-----+
個
用戶
| id | username | email | team_id |
+----+----------+-------+---------+
| 1 | user1 | | 1 |
+----+----------+-------+---------+
| 1 | user2 | | 2 |
+----+----------+-------+---------+
,我想下面的SQL查詢...
SELECT t.name as team_name, sum(s.score) as total_score
FROM teams t
JOIN users u ON u.team_id = t.id
JOIN stats s ON s.user_id = u.id
GROUP BY team_name ORDER BY total_score DESC
但上面的查詢將返回0行,即使我想你幫忙寫TP往上頂10用戶得分。
感謝您的幫助。
其他表的樣本數據請。 – Jacob
@cularis添加了更多示例數據。謝謝。 – seoppc