Jump to content

SUM query help


lukep11a

Recommended Posts

Hi, I have three tables, 'login', 'selections' and 'teams'. Each user is required to make 12 team selections which are stored in 'selections.teamid' their userid is stored in 'selections.userid'. All team id's are listed in 'teams.teamid', along with the number of points they have in 'teams.points'. What I am trying to do is create an overall table, with each users username from 'login.username' and the total number of points their 12 team selections have. Here is the code I currently have:

 

<?php

	$query = "SELECT login.userid, selections.userid, login.username, SUM(teams.points) FROM login, selections, teams WHERE login.userid = selections.userid AND selections.teamid = teams.teamid";

	$result = mysql_query($query) or die(mysql_error());

	while($row = mysql_fetch_assoc($result))
	{
		echo $row['username']. " - ".$row['SUM(teams.points)'];
		echo "<br />";
      	}

 	?>

 

At the minute the result is displaying the first username with an entry in the 'selections' table, and the total number of points from all entries in the 'selections' table regardless of 'userid'. And what I am trying to get is a list of all the usernames with an entry in the 'selections' table and the total number of points each user has. Hope that makes sense, any help would be greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/241316-sum-query-help/
Share on other sites

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.