我有两张桌子,一张叫做玩家,另一张叫奖励。最终用户向玩家提供“奖励”,玩家可以多次获得相同的奖励。
Awards Table
----+------------------------------------------------------------------------+
| ID | name | player_id |
+----+-----------------------------------------------------------------------+
| 1 | Free-throw Excerpt | 1 |
| 2 | Free-throw Excerpt | 6 |
| 3 | Top Earner | 1 |
| 4 | Top Player | 5 |
| 5 | Free-throw Excerpt | 1 |
| 6 | Free-throw Excerpt | 1 |
| 7 | Top Earner | 1 |
| 8 | Top Player | 1 |
...
+----+-----------------------------------------------------------------------
`Players Table`
----+------------------------------------------------------------------------+
| ID | name |
+----+-----------------------------------------------------------------------+
| 1 | Player A |
| 2 | Player B |
| 3 | Player C |
| 4 | Player D
... |
+----+-----------------------------------------------------------------------
在我的应用中,每个玩家都有一个页面,在该页面上,我想显示玩家赢得的所有奖励。例如,对于玩家B的页面:
玩家A统计信息:
我可以查询奖励表以获得给予玩家A的所有奖励,但我被困在这里并且不知道去哪里。我是否需要做一个COUNT(*)来获得每个奖励的数量? (1名为Top Player,3名为Free Throw Excerpt)。
答案 0 :(得分:4)
你是对的。使用Group By计数
Select a.Name, Count(*) AS Total From Awards a
Where a.PlayerID = 1
Group By a.Name
Order By Count(*) DESC