Praveen Prasad
Praveen Prasad

Reputation: 32107

incrementing a table column's data by one || mySql

iam having a table with columns like

id || counter

if i do something (some event) i want the counter's value(at a particular id) to increase by one , currently iam doing this :

    //get current value
    current_value = select counter from myTable where id='someValue'  

    // increase value
    current_value++   

   //update table with current value
    update myTable set counter=current_value where id='someValue';  

currently iam running 2 queries for this, please suggest me some way do it in one step.

Upvotes: 1

Views: 124

Answers (1)

Joe Mastey
Joe Mastey

Reputation: 27099

Just run the math in the database:

update myTable set counter = counter + 1 where id = 'someValue';

Upvotes: 8

Related Questions