For Programmers: Free Programming Magazines  


Home > Archive > PHP SQL > November 2005 > increase MySQL field value by 1? (quickly/easily)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author increase MySQL field value by 1? (quickly/easily)
John McClumpha

2005-11-21, 7:56 am

Hi there - just a quick query (pardon the pun)...

is there a simple (quick) way to increase the value of an integer
field in a nominated row by 1 other than first having to retrieve the
value and then UPDATE the appropriate field? - similar to doing a
$Variable++ in PHP?

Thanks in advance

--
John McClumpha
Darkness Reigns - http://www.incitegraphics.com.au/darkness/
rob@centralpoint.nl

2005-11-21, 7:56 am

Sure, increase it in the update query, example:
UPDATE hit SET counter+1 WHERE site='www.google.com'
(assuming that counter field is integer)

John McClumpha schreef:

> Hi there - just a quick query (pardon the pun)...
>
> is there a simple (quick) way to increase the value of an integer
> field in a nominated row by 1 other than first having to retrieve the
> value and then UPDATE the appropriate field? - similar to doing a
> $Variable++ in PHP?
>
> Thanks in advance
>
> --
> John McClumpha
> Darkness Reigns - http://www.incitegraphics.com.au/darkness/


Stefan Rybacki

2005-11-21, 7:56 am

rob@centralpoint.nl wrote:
> Sure, increase it in the update query, example:
> UPDATE hit SET counter+1 WHERE site='www.google.com'
> (assuming that counter field is integer)


Well, should be:
UPDATE hit SET counter=counter+1 WHERE site='www.google.com'

Regards
Stefan

>...

Pat Farrell

2005-11-21, 6:59 pm

John McClumpha wrote:
> is there a simple (quick) way to increase the value of an integer
> field in a nominated row by 1 other than first having to retrieve the
> value and then UPDATE the appropriate field? - similar to doing a
> $Variable++ in PHP?


Why not use the autoincrement feature of MySql?

--
Pat


John McClumpha

2005-11-22, 7:56 am

Thanks Rob - that's done the trick nicely :)

On 21 Nov 2005 04:38:20 -0800, rob@centralpoint.nl wrote:
[color=darkred]
>Sure, increase it in the update query, example:
>UPDATE hit SET counter+1 WHERE site='www.google.com'
>(assuming that counter field is integer)
>
>John McClumpha schreef:
>
--
John McClumpha
Darkness Reigns - http://www.incitegraphics.com.au/darkness/
John McClumpha

2005-11-22, 7:56 am

On Mon, 21 Nov 2005 14:36:08 +0100, Stefan Rybacki
<stefan.rybacki@gmx.net> wrote:

>rob@centralpoint.nl wrote:
>
>Well, should be:
> UPDATE hit SET counter=counter+1 WHERE site='www.google.com'


Thanks stefan... I actually made that adjustment to Rob's code without
even thinking ;)

--
John McClumpha
Darkness Reigns - http://www.incitegraphics.com.au/darkness/
Rik

2005-11-22, 7:56 am

Stefan Rybacki wrote:
> rob@centralpoint.nl wrote:
>
> Well, should be:
> UPDATE hit SET counter=counter+1 WHERE site='www.google.com'
>



Hmmz would this work?

UPDATE hit SET counter=counter+1 WHERE counter > 5

I have no way to test this quickly right now, but it would save a lot of
lines of code :-)
It's for a table displayed in a specific order, and order has te be
changeable.

Grtz,

Rik


rob@centralpoint.nl

2005-11-22, 7:56 am

It should work fine!

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com