Jump to content

Simple column addition and storing the computed value


Recommended Posts

I asked this question in another forum but didn't get a reply. Maybe it's a dumb question but i'm kind of stuck.

 

Here is my simple scenario. my beginner application stores test results as shown in the table below. Each section_id is for a section of the test that is worth 25 points total. I want to be able to add the points in the four sections for each user_id to get the total grade out of 100 for that user.

 

result_id      section_id             user_id                 points          
   1                50                    3                      10
   2                60                    3                      20
   3                70                    3                      15
   4                80                    3                      11  

 

How can i add 10+20+15+11 and store the result in a new column with the relevant user_id?

well it looks like u need create new field on players table lets say `pts`

 

then before u are inserting data into table that u showed do some checking quory like this

$check_result = mysql_query("SELECT COUNT(*) AS counter, SUM(points) AS total_pts  FROM stats WHERE user_id=3");
$row = mysql_fetch_assoc($check_result);
if($row['counter'] >= 4)
{
//if we have four entries in `stats` we should store total points somewhere before inserting new fight stats
mysql_query("UPDATE players SET pts = pts + ".$row['total_pts']." WHERE id=3 LIMIT 1");

//also I think u should remove old entries at this point
mysql_query("DELETE FROM stats WHERE user_id=3");
}

//now do what ever u was doing before I mean store new result into table that I called `stats`

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.