SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    cheshire
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I sum the numbers in a field

    I would like to add up the scores in a column in my table. I would like a little advice on the best way to do this. I have looked in the manual and see there is a function called array_sum(array arr). Would I be able to use this on the resulr set that comes back from a query which fetches the values from the column called 'scores'.
    When I get the basics of this working, I would like to go one step further and sum only some of the values in the column e.g. sum only those values that correspond to the chemistry test and not the physics test. For reasons which I can't quite remember, I found it more convenient to store test results from more than one type of test in the same column. I have another column called 'testname' which identifies the test as chemistry or physics. I think you can put something like 'WHERE testname LIKE '%Chemistry%' as a clause to limit what the query returns - is that correct.

  2. #2
    epsilon transition cupid's Avatar
    Join Date
    Aug 2001
    Location
    Kent, Ohio
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe I'm not understanding your question completely.. but perhaps this can be of use to you:

    I've always used the sum() function to sum up the columns:

    PHP Code:
    $query mysql_query("select sum(scores) as g from tablename where argument");

    echo 
    mysql_result($query,"g",0); 
    The sum of the field 'scores' will be displayed on your page

  3. #3
    SitePoint Evangelist
    Join Date
    Aug 2000
    Location
    Singapore
    Posts
    411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would like to do something like his, but sum up all the records in several columns. How do I do this and display it?

  4. #4
    SitePoint Member
    Join Date
    Sep 2001
    Location
    Glasgow, Scotland
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $blah mysql_fetch_array(mysql_query("SELECT sum(column1+column2) AS total FROM TABLE")); 
    then just use $blah[total]
    signature


Bookmarks

Posting Permissions

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