Jump to content

Recommended Posts

I am trying to reference values across two tables.  I think I have the correct query but need help referencing the values:

 

I can get the first and last name's of the first coach just fine but I cant figure out how to get the first and last name's of the second coach.

 

Here are the two tables in question:

 

teams

team_idcoach1coach2

20091317

20081412

 

coaches

 

coach_idfirst_namelast_name

12bobsmith

13johndoe

14jimjohnson

17fredwhite

 

Those are the two tables but imagine about 1000 more entries and even more columns (but they dont have anything to do with this query so I didnt list them).

 

Here is my php code:

 

$sql = "SELECT * from teams a, coaches b WHERE b.coach_id=a.coach1";
		$result = mysql_query($sql, $conn) or die($mysql_error());
		while ($team = mysql_fetch_array($result)) {
			$year = $team['team_id'];
			$coach_first = $team['first_name'];
			$coach_last = $team['last_name'];
			echo $year." - ".$coach_first." ".$coach_last."<br />";

 

Thanks a lot.

SELECT team_id,
       c1.first_name as coach1_fname, c1.last_name as coach1_lname,
       c2.first_name as coach2_fname, c2.last_name as coach2_lname,

FROM teams t

JOIN coaches c1 ON t.coach1 = c1.coach_id
JOIN coaches c2 ON t.coach2 = c2.coach_id

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.