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