Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-22-2010, 12:01 PM   PM User | #1
don1081
New Coder

 
Join Date: Dec 2010
Posts: 19
Thanks: 3
Thanked 0 Times in 0 Posts
don1081 is an unknown quantity at this point
Question Inserting value from one table to other table's column for specific user

Hello experts,

I need help. Please take a look at this code:
PHP Code:
$user_td $db->fetch_array($result);
$result $db->query("insert into ts_users(total_score) select sum(score) from ts_game_sessions where user_id = $user_id;"); 
This code is summing the all values of column "score" from tabel ts_game_sessions for "user_id". And then inserting sum of scores in column "total_score" of table ts_users.

Now the problem is, while inserting the summed value, it is creating a new user in ts_users and inserting summed value in "total_score" and autofilling new user details as NULL, NULL, NULL, NULL.

I want to insert summed value in "total_score" of existing user. How to do that?
don1081 is offline   Reply With Quote
Old 12-22-2010, 03:33 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
Do that with an UPDATE query. You'll need to do a SELECT query to get the sum(score) value first.

As an aside, it's typically bad practice to store totals. It's better to select sum(score) each time it's needed. Storing totals creates a lot of unnecessary overhead to keep the stored totals up-to-date. The only time it makes sense is when the load on your database becomes so heavy, the sum() queries are too much to handle and you need a performance boost. Even then, I would keep the totals up-to-date be use of a trigger on the game_sessions table, so any time the game_sessions table is changed (inserted into, deleted from, or updated) the total_score is updated too.
__________________
Fumigator is offline   Reply With Quote
Users who have thanked Fumigator for this post:
don1081 (12-23-2010)
Old 12-22-2010, 07:56 PM   PM User | #3
don1081
New Coder

 
Join Date: Dec 2010
Posts: 19
Thanks: 3
Thanked 0 Times in 0 Posts
don1081 is an unknown quantity at this point
Thanks for better suggestion. But the problem is i cannot able to display the total score in user profile. The only way i can display it by storing the total score first and calling it for display, i know it will be a load on server but i have no other option.
I will apreciate if you can help me in any way.
don1081 is offline   Reply With Quote
Old 12-22-2010, 08:24 PM   PM User | #4
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
You can't SELECT SUM(score) FROM ts_game_session when you display a profile? Why not?
__________________
Fumigator is offline   Reply With Quote
Users who have thanked Fumigator for this post:
don1081 (12-23-2010)
Old 12-23-2010, 11:17 AM   PM User | #5
don1081
New Coder

 
Join Date: Dec 2010
Posts: 19
Thanks: 3
Thanked 0 Times in 0 Posts
don1081 is an unknown quantity at this point
Thanks alot, now problem is solved and its displaying correctly.
Thanks for giving me time.
don1081 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:34 AM.


Advertisement
Log in to turn off these ads.