MySql adding or subtracting value by update statement

When we want to add or subtract some value like +1 or -1,
we can do like this( in the case we want to add 1)

1
update table name set column's name = column's name + 1;

This query adds 1 in all values of that column.
So we can use this like this

1
update table name set column's name = column's name +1 where column's name = A;

This query adds 1 if the column’s value is A.

ex) Waiting line in a bank

If new person came to line ‘6’ in the back,

1
update bank set waiting = waiting +1 where line_num = '6';

then we add 1 to waiting column in the row having 6 as its line_num column’s value.

Comments