Jump to content

Joining tables with two values


cliftonbazaar

Recommended Posts

$teamID=101;  #This is the ID of our own team

 

If I have table MATCHES

matchID, tournament, homeTeamID, awayTeamID, e.t.c

1, 1, 101, 98

2, 2, 98, 99

3, 3, 101, 100

4, 4, 99, 100

5, 4, 100, 101

 

and table TOURNAMENTS with the data

tournamentID, winner, loser

1, 101, 98

2, 99, 98

3, 101, 100

4, 99, 100

 

and table TEAMS with data

teamID, name

98,  Darren

99, Danika

100, Emma

101, James

 

How would I print out the following - "tournamentID, Winners name, losers name" - if the team was involved with the tournament?

Print out with this data would be-

1, James, Darren

3, James, Emma

4, Danika, Emma

 

I know that a JOIN is to be used but I can only find if for one link where as I need to link it to both the winner and loser.

 

At the moment my code is, but it doesn't work anymore and doesn't look for the losers name :(

echo "<TR><TH COLSPAN=8><font size='6'>PREVIOUS TOURNAMENTS</TH>";
	$tournaments=mysql_query("
		SELECT tournament FROM matches
		WHERE homeTeamID='$teamID' or awayTeamID='$teamID'
		GROUP BY tournament
		ORDER BY tournament desc
		");
	while($tournament=mysql_fetch_array($tournaments)) {
		$tournament=$tournament['tournament'];
		$tournament_details=mysql_fetch_array(mysql_query("
			SELECT *.tournament, teamID.teams, name.teams
			FROM tournaments
			INNER JOIN teams
			ON winner.tournament = teamID.teams
			"));
		echo "<TD align='center'>".$tournament_details['tournamentID']."</TD>";
		echo "<TD colspan=2>".$tournament_details['winner']."</TD>";
		echo "<TD colspan=2>".$tournament_details['loser']."</TD>";
	}
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.