Vote Up 0 Vote Down

Obtaining and change the data fields one mysql query

To change a numeric field first request data of the field, after receiving value makes a second request - to modify, after reducing the field to 1. I think you can optimize the process and make a query that will not prompt, and then update, and immediately reduce the value by one how would it have been (positive or negative).


Vote Up 0 Vote Down
Decrease the value in the field, you can:

UPDATE table SET number = number - 1 WHERE id = 5;

Only here it is necessary to bear in mind, if a field is declared as UNSIGNED, if in the number is 0, then there is an error. In this case, you can use this query:

UPDATE table set number = IF(number > 0, number - 1, 0) WHERE id = 5;
flag | link |
Vote Up 0 Vote Down
UPDATE table SET column = column - 1;
flag | link |

Your Answer

Login before answering

Login with facebook