lukep11a Posted July 7, 2011 Share Posted July 7, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241316-sum-query-help/ Share on other sites More sharing options...
gristoi Posted July 7, 2011 Share Posted July 7, 2011 you need to group by the userid Quote Link to comment https://forums.phpfreaks.com/topic/241316-sum-query-help/#findComment-1239543 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.