Jump to content


Photo

Adding to a counter value in a db


  • Please log in to reply
2 replies to this topic

#1 shaggycap

shaggycap
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 11 May 2006 - 10:55 AM

I have a table that I want to update a counter field. However I dont want to use update as it will replace the value, I want to add the new value onto the existing, to increment the value by that amount.

Can I do this with an update statement? or will I have to pull the exisiting value out first and add them up in my code? Just thought there may be a function to do this, as it seems quite a common task.

Thanks

#2 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 12 May 2006 - 09:07 PM

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']UPDATE[/span] table SET field=field+$new_value WHERE condition [!--sql2--][/div][!--sql3--]
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#3 shaggycap

shaggycap
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 13 May 2006 - 04:49 PM

[!--quoteo(post=373320:date=May 12 2006, 04:07 PM:name=ryanlwh)--][div class=\'quotetop\']QUOTE(ryanlwh @ May 12 2006, 04:07 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']UPDATE[/span] table SET field=field+$new_value WHERE condition [!--sql2--][/div][!--sql3--]
[/quote]

Great stuff, thank you ryanlwh [img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /]





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users