MySQL查询团队计划

时间:2012-07-17 18:23:29

标签: php mysql

我正在尝试根据下面的表格检索每个团队的时间表,但是我在显示团队名称时遇到了一些麻烦。这是我的表格:

TEAM (TEAM_ID, 队名, team_mascot, 等)

GAME (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等结果?非常感谢提前。

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

select game_home_team, home.team_name AS homename,
    game_visitor_team, visitor.team_name AS visitorname
etc...