Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2005
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    adding values of a single column

    how can i add all the values of one column of the table to get a total?

  2. #2
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Code:
    SELECT SUM(myColumn) FROM myTable WHERE 1

  3. #3
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,665
    Thanks
    6
    Thanked 150 Times in 141 Posts
    Note that the WHERE 1 is not necessary.

  4. #4
    New Coder
    Join Date
    Mar 2005
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    cool. now how do i store that sum in a variable?

  5. #5
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    I guess you are using php?
    PHP Code:
    $sql "SELECT SUM(myColumn) FROM myTable";
    $result mysql_query($sql);
    list(
    $sum) = mysql_fetch_row($result);   // now $sum = 'the 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
  •