Discussion:
MySQL Increment/Decrement
Ben Miller
2009-12-29 02:54:04 UTC
Permalink
I hope this isn't a bone-head question - Is there a MySQL query that will
increment/decrement the value in an integer column with a single query - in
other words, I don't have to run a SELECT query to get the value,
add/subtract to/from the value, and then run an UPDATE query to store the
new value?



Thanks in advance.



Ben
Robert Cummings
2009-12-29 03:51:12 UTC
Permalink
Post by Ben Miller
I hope this isn't a bone-head question - Is there a MySQL query that will
increment/decrement the value in an integer column with a single query - in
other words, I don't have to run a SELECT query to get the value,
add/subtract to/from the value, and then run an UPDATE query to store the
new value?
You mean:

UPDATE table SET column = column + 1;

?

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Bipper Goes!
2009-12-29 03:51:47 UTC
Permalink
UPDATE SQLTABLE SET count = (count+1) WHERE PromoID=1

Is this valid for your issue? I have no way of testing or toying
Post by Ben Miller
I hope this isn't a bone-head question - Is there a MySQL query that will
increment/decrement the value in an integer column with a single query - in
other words, I don't have to run a SELECT query to get the value,
add/subtract to/from the value, and then run an UPDATE query to store the
new value?
Thanks in advance.
Ben
Eric Lee
2009-12-29 03:53:13 UTC
Permalink
Ben

It seems that you can just update the column with a update query like this,

update table set field = field + 1 where some condition

This might be the thing you need.


Eric
Post by Ben Miller
I hope this isn't a bone-head question - Is there a MySQL query that will
increment/decrement the value in an integer column with a single query - in
other words, I don't have to run a SELECT query to get the value,
add/subtract to/from the value, and then run an UPDATE query to store the
new value?
Thanks in advance.
Ben
muzy
2009-12-29 08:10:34 UTC
Permalink
Hello Ben,

I had the same question yesterday (but with SQLite) and there are at
least 2 solutions.

The first was already mentioned:

UPDATE table SET value = value + 1 WHERE foo = bar;

The second solution which also works is:

UPDATE table SET value = (SELECT value FROM table WHERE foo = bar) + 1
WHERE foo = bar;


I hope it helps.


Greetings from Germany,


Sebastian
Post by Ben Miller
I hope this isn't a bone-head question - Is there a MySQL query that will
increment/decrement the value in an integer column with a single query - in
other words, I don't have to run a SELECT query to get the value,
add/subtract to/from the value, and then run an UPDATE query to store the
new value?
Thanks in advance.
Ben
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Phpster
2009-12-29 12:31:52 UTC
Permalink
Post by muzy
Hello Ben,
I had the same question yesterday (but with SQLite) and there are at
least 2 solutions.
UPDATE table SET value = value + 1 WHERE foo = bar;
UPDATE table SET value = (SELECT value FROM table WHERE foo = bar) +
1 WHERE foo = bar;
I hope it helps.
Greetings from Germany,
Sebastian
Post by Ben Miller
I hope this isn't a bone-head question - Is there a MySQL query that will
increment/decrement the value in an integer column with a single query - in
other words, I don't have to run a SELECT query to get the value,
add/subtract to/from the value, and then run an UPDATE query to store the
new value?
Thanks in advance.
Ben
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
You may run into race conditions and contention when handling auto-
incrementimg this way. Best to isolate the SQL into a transaction to
avoid this

Bastien

Sent from my iPod
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...