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 to the CF scene
    Join Date
    Jul 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    MYSQL: arithmetic operation heeellp!!

    helo guys nice to be back here,

    i got this tiny weenie problem in my DB,

    wel im new to mysql too, and i just wnt to know how to add all the numerical values inside a single column. ?

    | id | c_code | date | peso |
    +----+--------+------------+------+
    | 74 | 10 | 1254-07-08 | 564 |
    | 75 | 11 | 1235-05-06 | 23 |
    | 78 | 4 | 1236-08-07 | 564 |
    | 79 | 5 | 1254-07-08 | 564 |
    | 82 | 12 | 1235-08-07 | 654 |
    | 83 | 13 | 1245-05-06 | 321 |
    | 84 | 1 | 1236-08-07 | 654 |

    let say i have this table. and i want to add all the values in the peso column..
    can anyone share thier expertise on this, i only know the basic operation. i tried learning stored procedures. and im still new to it, please guys i would really appreciate your reply, tnx.

  • #2
    Regular Coder
    Join Date
    Apr 2004
    Location
    Philadelphia
    Posts
    241
    Thanks
    2
    Thanked 5 Times in 5 Posts
    Hmm. Not sure if you're using PHP but if you are I think this may help.
    PHP Code:
    //Connected to DB already.
    $query1 mysql_query("SELECT * FROM table_name")  //Change table_name!!
    while($row mysql_fetch_assoc($query1)){
         
    $total_peso += $row[peso];
    }
    echo 
    'The total of the column "peso" is ' $total_peso
    Let me know if it works.

  • #3
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    or alternatively
    SELECT SUM(columnname) AS total FROM table;

  • #4
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hello guys thnks for the answers. yah im using php.

    hehe i have a follow up question. how about adding specific id in your table. using sum function. can i do this select sum(peso) as total from <table> where id = this and id = this?
    thnks.

  • #5
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    how about trying it? All it is is a select statement so you aren't going to break anything.


  •  

    Posting Permissions

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