Jump to content

MySQL Query to Retrieve Team Schedule


RyanMinor

Recommended Posts

I am trying to retrieve schedules for each team based on my table below, but I am having some trouble getting the team names to display. Here are my tables my table:

 

TEAM

team_id

team_name

team_mascot

etc.

 

GAME

game_id

game_date

game_home_team

game_visitor_team

game_home_score

game_visitor_score

game_complete

 

Here is my current query. I am able to get the team's id values with this but I need the team names as id values won't do me much good.

 

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;

 

How can I retrieve the team names with this query and also display a result such as W or L depending on the score for the game? Thanks so much in advance.

Link to comment
Share on other sites

Something like this

 

SELECT home.team_name, 
CASE WHEN game_home_score > game_visitor_score THEN 'W' WHEN game_home_score < game_visitor_score THEN 'L' ELSE 'D' as home_result,
visitor.team_name, 
CASE WHEN game_home_score > game_visitor_score THEN 'L' WHEN game_home_score < game_visitor_score THEN 'W' ELSE 'D' as visitor_result,
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 = 6 OR game_visitor_team = 6
ORDER BY game_date ASC;

Link to comment
Share on other sites

SELECT home.team_name, visitor.team_name, game_date, IF(game_home_score > game_visitor_score, 'W', 'L') AS WinLose
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;

 

EDIT: use Barand's solution, it's more extensive.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.