Jump to content

Recommended Posts

 

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 ?

 

 

Link to comment
https://forums.phpfreaks.com/topic/169775-solved-loop-db-pull-question/
Share on other sites

$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 />";
}

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.