imarockstar Posted August 11, 2009 Share Posted August 11, 2009 Hey guys ... well i am trying to add up some columns (the_value) based on a group Id ... lets say I have a DB set up like this : id the_value group_number ------------------------------------------------------ 1 3 105 2 4 105 3 5 105 4 1 110 5 2 110 6 5 110 7 4 115 8 6 115 9 5 115 so basically what I am wanting to do is add up the THE_VALUE of each row based on the GROUP_NUMBER. so as you can see with the above table layout there are 3 groups 105, 110 and 115. So I would need to add the 3 rows per group and display the sum of the 3 rows per group, THE_VALUE. for instance, the sum of the rows for GROUP_NUMBER 105 would be = 12 the sum of the rows for GROUP_NUMBER 110 would be = 8 the sum of the rows for GROUP_NUMBER 110 would be = 15 I now we would prob use a FOR loop but I am not sure how to group the rows then do the FOR loop .. can anyone help me ? Quote Link to comment https://forums.phpfreaks.com/topic/169775-solved-loop-db-pull-question/ Share on other sites More sharing options...
JonnoTheDev Posted August 11, 2009 Share Posted August 11, 2009 $result = mysql_query("SELECT group_number, SUM(the_value) AS total FROM myTable GROUP BY group_number ORDER BY group_number ASC"); while($row = mysql_fetch_assoc($result)) { print $row['group_number']." - ".$row['total']."<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/169775-solved-loop-db-pull-question/#findComment-895656 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.