SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Location
    Houston, TX
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question A better way to sum columns and add value to another column?

    Hi,

    I have a table in which each row contains numbers (int). I want to add (sum) some column values together and update the ttlpts column with a running total.

    Here is what I'm using which works. I'm just wanting to learn a better way which I assume exists:

    PHP Code:
    //sum columns for a user and add them to ttlpts.
    $result mysql_fetch_array(mysql_query("SELECT SUM
    (ask+best_ans+no_best+ans+del) as ttl, ttlpts, userid 
    FROM review_points 
    GROUP BY userid 
    HAVING userid = '
    $id' "));

    $ttl $result['ttl'];
    $ttlpts $result['ttlpts'];

    //add the column values to the ttlpts column
    $tp $ttl $ttlpts;

    mysql_query("UPDATE review_points 
    SET ttlpts = '
    $tp
    WHERE userid = '" 
    mysql_escape_string($id) . "'"); 
    Thanks for any tips!

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    If the userid is unique in the review_points table, you can do the following:

    Code:
    UPDATE review_points 
    SET ttlpts = ttlpts+ask+best_ans+no_best+ans+del
    WHERE userid = '" . mysql_escape_string($id) . "'"

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,248
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    running total?

    tim, please show a few rows of sample data
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Location
    Houston, TX
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    guido2004, thank you...that worked perfectly and is so much shorter!

    r937, I'm creating a scoring system similar to that used by yahoo answers (http://answers.yahoo.com/info/scoring_system).

    When a new user signs up 100 pts. are awarded. Then the other fields are for points earned, say for example, writing a review. Each day all the fields will be reset to '0' except for the id, userid, signup and ttlpts.

    Here is my table and data:

    Code:
    CREATE TABLE IF NOT EXISTS `review_points` (
      `id` int(11) NOT NULL auto_increment,
      `userid` int(9) NOT NULL,
      `signup` int(11) NOT NULL,
      `ask` int(11) NOT NULL,
      `best_ans` int(11) NOT NULL,
      `no_best` int(11) NOT NULL,
      `ans` int(11) NOT NULL,
      `del` int(11) NOT NULL,
      `login` int(11) NOT NULL,
      `vote_best` int(11) NOT NULL,
      `vote_no` int(11) NOT NULL,
      `give_best` int(11) NOT NULL,
      `thumb_up` int(11) NOT NULL,
      `ttlpts` int(11) NOT NULL,
      PRIMARY KEY  (`id`),
      UNIQUE KEY `userid` (`userid`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
    
    --
    -- Dumping data for table `review_points`
    --
    
    INSERT INTO `review_points` (`id`, `userid`, `signup`, `ask`, `best_ans`, `no_best`, `ans`, `del`, `login`, `vote_best`, `vote_no`, `give_best`, `thumb_up`, `ttlpts`) VALUES
    (4, 3, 100, 5, 5, -2, 5, 0, 0, 0, 0, 0, 0, 139);
    Thanks,

    Tim

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,248
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    ah, that kind of running total

    i though you might have meant this kind of running total

    trans amt balance
    deposit 50 50
    withdraw 10 40
    withdraw 20 20
    deposit 60 80
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •