From: Phpster on




On Dec 29, 2009, at 3:10 AM, muzy <php(a)muzybot.de> wrote:

> 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
>
> Ben Miller wrote:
>> 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
First  |  Prev  | 
Pages: 1 2
Prev: SQL Queries
Next: Merry Xmas and Happy New year