Goarge Go Goarge Go - 7 months ago 17
SQL Question

update data in mysql column field without removing previous value

i am trying to update "new" column value with new value but problem is my quary remove previous data while inserting new value

what is want : Here is example table structure,

Table name = agg_lvl primary key set = uid
uid | new
--------|--------
1 | 100
2 | 300


you can see "new" has 100 points, for example i send 100 new points to user 1, so new column value should be 100 + 100 = 200, right now with this code

$query4 = mysql_query("INSERT INTO agg_lvl (uid, new) VALUES ('$uid','$new')
ON DUPLICATE KEY UPDATE uid='$uid',new='$new'");


not sure what new='$new'
i have tried both ways but no success = >

new='new+$new' or new=new+'$new'

Answer

Okay first i will answer with the proper way to do the same, In this case i am assuming that UID is unique, so you make a new table scorecard with UID as foreign key. Now rather than update, you just insert stuff to table like if UID 1 gains 10 and 20 points, there are two entries. onw with 10 and one with 20. Now to get his current points, you add all points where UID=1 .

Now in your implementation the correct query would be

UPDATE userData SET points = points + x WHERE UID = $uid

where x is the new points gained and points is the name of column