1

我正在尝试根据下表检索每个团队的日程安排,但在显示团队名称时遇到了一些问题。这是我的表:

团队(team_id、team_name、team_mascot 等)

游戏(game_id、game_date、game_home_team、game_visitor_team、game_home_score、game_visitor_score、game_complete)

这是我当前的查询。我可以通过这个获得团队的 id 值,但我需要团队名称,因为 id 值对我没有多大好处。

SELECT game_home_team, game_visitor_team, game_date
FROM game 
INNER JOIN team home ON home.team_id = game_home_team 
INNER JOIN team visitor ON visitor.team_id = game_visitor_team
WHERE game_home_team = ? 
OR game_visitor_team = 6 
ORDER BY game_date ASC;

如何使用此查询检索球队名称,并根据比赛得分显示 W 或 L 等结果?提前非常感谢。

4

2 回答 2

3
SELECT
   home.team_name AS home_team,
   visitor.team_name AS visitor_team,
   game_date,
   IF(
      game_home_score = game_visitor_score, 
      'Tie', 
      IF(
         game_home_score > game_visitor_score,
         'Hometeam',
         'Visitorteam'
      )
   ) AS Winner
FROM game 
INNER JOIN team home ON home.team_id = game_home_team 
INNER JOIN team visitor ON visitor.team_id = game_visitor_team
WHERE game_home_team = ? 
OR game_visitor_team = 6 
ORDER BY game_date ASC;
于 2012-07-17T18:27:22.637 回答
0
select game_home_team, home.team_name AS homename,
    game_visitor_team, visitor.team_name AS visitorname
etc...
于 2012-07-17T18:27:54.900 回答