Jump to content

Help joining these two tables


msaz87

Recommended Posts

Hey all,

 

I was hoping to get some help joining these two tables...

 

The first table is called "standing"

standing.png

 

And this one is "game"

game.png

 

I didn't set these tables up, so they're not designed to make my life easier, but I'll try to explain what's going on:

 

Each game (game_id) has team_1 and team_2 and their respective scores are kept in the standing table, along with whether they won or lost (loose wasn't my typo...) and what I'm looking to do with joining them is to be able to output all the games in a week WHERE the location_id and league_id match my criteria, but the games would output with team_1, team_2 and their pts_scored.

 

Using the two shots above, here's an example:

 

TEAM 1.....PTS SCORED

50............54

46............7

 

51............37

45............15

 

Any suggestions and help is greatly appreciated.. thanks!

Link to comment
Share on other sites

Hi

 

Nearest I can see would be this

 

SELECT game.game_id, team_1, pts_scored, '1' AS teamOrder
FROM game
INNER JOIN standing
ON game.game_id = standing.game_id
AND game.team_1 = standing.team_id
WHERE location_id = '$location'
AND league_id = '$league'
UNION
SELECT game.game_id, team_2, pts_scored, '2' AS teamOrder
FROM game
INNER JOIN standing
ON game.game_id = standing.game_id
AND game.team_2 = standing.team_id
WHERE location_id = '$location'
AND league_id = '$league'
ORDER BY game_id, teamOrder

 

This is simpler but gives you both scores for a game on 1 row.

 

SELECT game.game_id, team_1, standing1.pts_scored, team_2, standing2.pts_scored
FROM game
INNER JOIN standing1
ON game.game_id = standing1.game_id
AND game.team_1 = standing1.team_id
INNER JOIN standing2
ON game.game_id = standing2.game_id
AND game.team_2 = standing2.team_id
WHERE location_id = '$location'
AND league_id = '$league'

 

All the best

 

Keith

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.