-3

如果有人可以帮助我为我的表找到正确的查询,我将不胜感激,因为我经常尝试但没有奏效。

4

2 回答 2

1

1)

select
  Case when Team.Team_id = Game.Guest_team_id then 'Guest' else 'Home' end as location,
  Team_name,
  Player_name
from Game
join Team on Team.Team_id in (Game.Home_team_id,Game.Guest_team_id)
join Player on Player.team_id = Team.Team_id
order by 1,3

2) 假设这score是扣除 Home_team 得分 - Guest_team 得分:

select Team_Name, TotalPoints = sum(Points)
from (
  select 
    Team_Name, case when score > 0 then 2  
                    when score = 0 then 1 
                                   else 0 end as Points
  from Team join Game on Team.Team_id = Game.Home_team_id
  union all
  select 
    Team_Name, case when score < 0 then 2 
                    when score = 0 then 1 
                                   else 0 end as Points
  from Team join Game on Team.Team_id = Game.Guest_team_id
) T
GROUP BY Team_Name
order by 2 desc
LIMIT 1

这很容易修改以计算任何特定的感兴趣的结果变化。

更新- 澄清后score

select Team_Name, TotalPoints = sum(Points)
from (
  select 
    Team_Name, case when home_team_score > guest_team_score then 1
                                                            else 0 
               end as Points
  from Team join Game on Team.Team_id = Game.Home_team_id
  union all
  select 
    Team_Name, case when home_team_score < guest_team_score then 1
                                                            else 0 
               end as Points
  from Team join Game on Team.Team_id = Game.Guest_team_id
) T
GROUP BY Team_Name
order by 2 desc
LIMIT 1
于 2013-05-02T03:53:17.897 回答
0

我会让你休息一下,因为 MySQL 一开始可能会很困难而且很混乱。您应该研究这些查询是如何工作的。如果您正在学习 SQL:我个人最有效的学习方式是通过大量练习,这些练习让我思考不同类型的 SQL 查询 - 所以做很多练习。

两队球员姓名

如何通过名字显示两支球队的所有球员?

我假设你的意思是团队,而不是游戏。给定一个游戏,这样做:

SELECT Player.team_id AS team_id,
    Player.player_name AS player_name
FROM Player
INNER JOIN Game
    ON (
        (Game.Home_team_id = Player.team_id)
        OR (Game.Guest_team_id = Player.team_id))
WHERE Game.game_id = [YOUR GAME ID];

这将产生如下结果:

team_id     player_name
1           John Smith
1           Jane Doe
2           John Person

获胜次数最多的球队

如何找出胜率高于其他球队的球队?

由于似乎没有任何说明谁赢了,我不能真正告诉你如何编写这样的查询。只有一个分数字段,我不确定它包含什么。我也不能真正绕开这个问题,因为查询看起来完全不同,具体取决于可用于工作的数据,而且我不喜欢做出假设(这可能对我们俩来说都是浪费时间)。

例如,如果它包含 JSON 或 XML 数据,或包含一组分数的其他数据格式,则需要将其拆分为自己的单元格。将数据集合存储在表格单元格中,例如分数的 JSON 集合,这很好 - 只要您永远不需要查询它。一旦你需要查询它,就会变成一团糟,你需要避免这种情况。

于 2013-05-02T03:52:01.583 回答