Sum for all value in table field(int)

Discussion in 'PHP' started by junandya, Dec 30, 2007.

  1. #1
    hi,

    i have some problem with my php script. i want to sum all value in my table field, here it is the field that i have:

    table name examVal
    ratingValue ---- int(2)
    Tony -------------5
    Linda -------------9
    Burhan -----------7
    Darsimin ----------10
    Tukiyem ----------4
    Bajeneh ----------9

    And here the is query

    $queryRating = mysql_query ("select int from examVal",$connection) or die (mysql_error());
    PHP:
    And now my question is: how to get a value like this (what is the script):
    5+9+7+10+4+9=44, should it use array?

    Best Ragards
    S.Junandya
     
    junandya, Dec 30, 2007 IP
  2. Dagon

    Dagon Active Member

    Messages:
    122
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #2
    $queryRating = mysql_query ("select sum(ratingValue) from examVal",$connection) or die (mysql_error());
     
    Dagon, Dec 30, 2007 IP
  3. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #3
    MySQL has what's known as Aggregate Functions.

    The function you're looking for is SUM.

    You might want to use an alias for the result of the function, it makes it easier to reference.
    SELECT SUM(column) as column_sum FROM table_name
    Code (markup):
    // Assume $row is the result row here
    $sum = $row['column_sum'];
    PHP:
     
    joebert, Dec 30, 2007 IP