2012-07-17 93 views
1

我想根據下表獲取每個團隊的計劃,但我在獲取團隊名稱時顯示出一些問題。這裏是我的表:針對團隊計劃的MySQL查詢

TEAM (TEAM_ID, TEAM_NAME, 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視得分爲遊戲?非常感謝提前。

+0

哪支球隊你想賣,遊客或家庭的W和L? – DrinkJavaCodeJava 2012-07-17 18:29:37

回答

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; 
+0

感謝您的快速回復。我現在面臨的問題是home_team和visitor_team都顯示爲相同。 – WebDev84 2012-07-17 18:37:56

+0

沒關係,我明白了。謝謝! – WebDev84 2012-07-17 18:56:43

+0

@ RCM8402如果你對SO的回答感到滿意,你應該[接受](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)回答,看看你迄今未接受任何答案 – 2012-07-17 20:22:21

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