Jump to content

Help with JOIN query


lukep11a

Recommended Posts

Hi, I have two tables, one called 'selections' and one called 'teams' structured like this:

 

selections

id

username

section1

section2

section3

 

teams

id

team

group

points

 

I am trying to display team names selected by the user that has logged in from the selections table followed by the points each team has from the teams table. The code below is not quite right, it displays each team name selected by the user repeatedly for each entry in the teams table, any help trying to fix the code below would be much appreciated.

 

<?php

	$query = "SELECT * FROM selections, teams WHERE selections.username = '{$_SESSION['username']}'";
	$result = mysql_query($query) or die(mysql_error());

	while($row = mysql_fetch_assoc($result))
	{
		echo $row['section1']. " - ". $row['points'];
		echo "<br />";
		echo $row['section2']. " - ". $row['points'];
		echo "<br />";
		echo $row['section3']. " - ". $row['points'];
		echo "<br />";
      	}
 	?>

Link to comment
Share on other sites

I think I need to do something like the code below to specify which teams are pulled out of the 'teams' table:

 

<?php

	$query = "SELECT * FROM selections, teams WHERE selections.username = '{$_SESSION['username']}' AND (selections.section1 = teams.team OR selections.section2 = teams.team OR selections.section3 = teams.team)";
	$result = mysql_query($query) or die(mysql_error());

	while($row = mysql_fetch_assoc($result))
	{
		echo $row['section1']. " - ". $row['points'];
		echo "<br />";
		echo $row['section2']. " - ". $row['points'];
		echo "<br />";
		echo $row['section3']. " - ". $row['points'];
		echo "<br />";
      	}
 	?>

 

But something is still not quite right as this is the result that is displayed:

 

Man Utd - 150

Liverpool - 150

Aston Villa - 150

Man Utd - 180

Liverpool - 180

Aston Villa - 180

Man Utd - 210

Liverpool - 210

Aston Villa - 210

 

Does anybody know how I can amend the code to get each team only once??

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.