MySQL Increment/Decrement

------=_NextPart_000_00C8_01CA87F7.82724840
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

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


------=_NextPart_000_00C8_01CA87F7.82724840--
Ben Miller [ Di, 29 Dezember 2009 03:54 ] [ ID #2027351 ]

Re: MySQL Increment/Decrement

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?

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
Robert Cummings [ Di, 29 Dezember 2009 04:51 ] [ ID #2027352 ]

Re: MySQL Increment/Decrement

--0016e6d645a8f6532b047bd5f05e
Content-Type: text/plain; charset=ISO-8859-1

UPDATE SQLTABLE SET count = (count+1) WHERE PromoID=1

Is this valid for your issue? I have no way of testing or toying

On Mon, Dec 28, 2009 at 8:54 PM, Ben Miller <ben [at] tottd.com> 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
>
>

--0016e6d645a8f6532b047bd5f05e--
bipper.goes [ Di, 29 Dezember 2009 04:51 ] [ ID #2027353 ]

Re: MySQL Increment/Decrement

--00504501764a18039b047bd5f646
Content-Type: text/plain; charset=UTF-8

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



On 12/29/09, Ben Miller <ben [at] tottd.com> 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
>
>

--00504501764a18039b047bd5f646--
Eric Lee [ Di, 29 Dezember 2009 04:53 ] [ ID #2027354 ]

Re: MySQL Increment/Decrement

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
muzy [ Di, 29 Dezember 2009 09:10 ] [ ID #2027357 ]

Re: MySQL Increment/Decrement

On Dec 29, 2009, at 3:10 AM, muzy <php [at] 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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Phpster [ Di, 29 Dezember 2009 13:31 ] [ ID #2027361 ]
PHP » gmane.comp.php.general » MySQL Increment/Decrement

Vorheriges Thema: Noob stuff - Zend/Opcode/Cache/Optimizer
Nächstes Thema: [php] Question about jsmin-php code