Results 1 to 6 of 6

Thread: MySQL (TOTAL)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    MySQL (TOTAL)

    I have this table:

    ID SUM
    1 2
    2 5
    3 3


    There is any function to give me the total of SUM column:
    2+5+3=10

    I can calculate them using math such as $i=$i+$row[sum] .. but I ask about function to do that auto.

    Thanks...

  2. #2
    scoutt
    Guest
    you could put those into an array and then add them that way using array_sum()

    or when you pull them out of the DB you can add them to each other like you are doing.

  3. #3
    New Member
    Join Date
    Jul 2001
    Posts
    14
    I think you can also use:


    select sum(columnName) as variable from table;

    You can use that query and i think the value will be stored in the variable $variable.

  4. #4
    Fanatic Member ubunreal69's Avatar
    Join Date
    Apr 2001
    Location
    Morayfield, Australia
    Posts
    609
    or just run a loop that will add them together. I'll post an example in a moment

  5. #5
    Fanatic Member ubunreal69's Avatar
    Join Date
    Apr 2001
    Location
    Morayfield, Australia
    Posts
    609
    wouldn't something like this work:
    PHP Code:
    $x 0;
    $the_sum 0;
    While (
    $x mysql_num_rows($query))
    {
    $the_sum $the_sum mysql_result($query,$x,"sum");
    $x++;
    }

    ECHO 
    "Sum of row: $the_sum"
    ?

  6. #6
    scoutt
    Guest
    yup that could work but HeaveNGuY has the right idea. besides using mysql_result is a lot slower then using sum()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width