douyi1966 2012-11-05 18:41
浏览 23
已采纳

在SQL中,我如何获得每个玩家的奖励总额

I have two tables, one called Players and one called Awards. End-users give an "award" to a player, and players can receive the same award multiple times.

 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 
       ...                                                                   |  
+----+-----------------------------------------------------------------------

In my app, each player has a page, and on that page, I want to display all the awards that the player has won. For example, for Player B's page:

Player A Stats:

  • Free Throw Excerpt: 3 votes
  • Top Earner: 2 Votes
  • Top Player: 1 Vote

I can query the the awards table to get all the awards given to Player A, but I'm stuck here and don't know where to go. Do I need to then do a COUNT(*) to get the number of each award received? (1 for Top Player, 3 for Free Throw Excerpt).

  • 写回答

1条回答 默认 最新

  • doupang4126 2012-11-05 18:44
    关注

    You are right. Count with Group By will do it

    Select a.Name, Count(*) AS Total From Awards a
    Where a.PlayerID = 1
    Group By a.Name
    Order By Count(*) DESC
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!