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
https://forums.phpfreaks.com/topic/241010-help-with-join-query/
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??

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.