I am using following tables…
stats (id, game, user_id, rank, score, dnt)
teams(id, name, dnt)
users(id, username, email, team_id, dnt)
I want to grab top 10 high scorer teams based on stats(team total score gets calculated on total score by its users)
sample data for stats…
| id | game | user_id | rank | score | dnt |
+----+------+---------+-------+------+-----+
| 1 | test | 5 | 2 | 2200 |
+--------+----------+----------+-----+-----+
| 2 | test | 3 | 1 | 2500 |
+--------+----------+----------+-----+-----+
teams
| id | name | dnt |
+----+-------+-----+
| 1 | team1 | |
+----+-------+-----+
| 2 | team2 | |
+----+-------+-----+
users
| id | username | email | team_id |
+----+----------+-------+---------+
| 1 | user1 | | 1 |
+----+----------+-------+---------+
| 1 | user2 | | 2 |
+----+----------+-------+---------+
And i am trying following sql query…
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
But above query is returning 0 rows, even i want your help tp write up top 10 users score.
Thanks for help.
Your
user_idinstatstable is nowhere to be found in youruserstable. You even have 2 users with the sameid? Your query returns the correct result.