This is a small tip which can help you optimize your MySQL query. Since you do not have to first “read” the record, instead you can increment it right there. I have seen some newbies doing the read and then increment process, which uselessly increases a select query to the database, each time you have to update a certain record.

This is sleek way to increment or decrement a record, of course the integer value 🙂

Let us suppose I need to increment the visitors count on my website in a table, this is how I can write the increment query –

 

Update visitors set visits=visits+1;

And if you want to decrease a record just change + to – , simple arithmetic ?

 

Hope that this simple, newbie trick helps you in some way.

Stay Digified !!
Sachin Khosla

Share this post: