By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,776 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

best way to...

P: n/a
hi all,

I want to decrement all the rows in a column that contain a number that is
bigger then one defined by me.

Is there some function in mysql or php that makes this task easy or do I
have to check row per row and if true decrement the row?

kind regards
Stijn
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Fri, 14 Nov 2003 18:57:25 +0100, Stijn Goris wrote:
hi all,

I want to decrement all the rows in a column that contain a number that is
bigger then one defined by me.

Is there some function in mysql or php that makes this task easy or do I
have to check row per row and if true decrement the row?

kind regards
Stijn

$sql = "
UPDATE database.table
SET field = field - 1
WHERE field > '$max'
";
Untested.. but should work =)

Regards,

Ian

--
Ian.H [Design & Development]
digiServ Network - Web solutions
www.digiserv.net | irc.digiserv.net | forum.digiserv.net
Programming, Web design, development & hosting.

Jul 17 '05 #2

P: n/a
Stijn Goris wrote:
I want to decrement all the rows in a column that contain a number that is
bigger then one defined by me.

Is there some function in mysql or php that makes this task easy or do I
have to check row per row and if true decrement the row?

kind regards
Stijn


Not sure if this is what you're looking for, but by the sounds of it,
you can do this with a simple update query...

UPDATE table_name SET field_name = field_name -1 WHERE field_name >
my_number

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Jul 17 '05 #3

P: n/a
Justin Koivisto wrote:
Stijn Goris wrote:
I want to decrement all the rows in a column that contain a number
that is
bigger then one defined by me.

Is there some function in mysql or php that makes this task easy or do I
have to check row per row and if true decrement the row?

kind regards
Stijn


Not sure if this is what you're looking for, but by the sounds of it,
you can do this with a simple update query...

UPDATE table_name SET field_name = field_name -1 WHERE field_name >
my_number


Once again, I should have checked for newly posted messages before
replying.... ;)

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Jul 17 '05 #4

P: n/a

"Ian.H" <ia*@WINDOZEdigiserv.net> wrote in message
news:pa****************************@hybris.digiser v.net...
On Fri, 14 Nov 2003 18:57:25 +0100, Stijn Goris wrote:
hi all,

I want to decrement all the rows in a column that contain a number that is bigger then one defined by me.

Is there some function in mysql or php that makes this task easy or do I
have to check row per row and if true decrement the row?

kind regards
Stijn

$sql = "
UPDATE database.table
SET field = field - 1
WHERE field > '$max'
";
Untested.. but should work =)

Regards,

Ian

--
Ian.H [Design & Development]
digiServ Network - Web solutions
www.digiserv.net | irc.digiserv.net | forum.digiserv.net
Programming, Web design, development & hosting.


thanks,

I didn't know sql could do those operations.

thanks
Jul 17 '05 #5

P: n/a
Justin Koivisto (31.596% quality rating):

UPDATE table_name SET field_name = field_name -1 WHERE field_name >
my_number


Note: if field_name is a unique field or key, you'll need to add an
ORDER BY clause to make sure this works.

/joe
--
In City Cafe, Brian Culver celebrates with the nameserver from Dramatech,
and then emasculates the lethal, aggravating frat house from the Deck(tm).
In 101, Keith links to the website of Kramer for the fiber channel, and
then stimulates a gombizler. Scott Watkins felches Taco ... [tape runs out]
Jul 17 '05 #6

P: n/a
On Fri, 14 Nov 2003 18:45:06 +0000 (UTC), Disco Plumber
<sc**@moralminority.org> wrote:
Justin Koivisto (31.596% quality rating):

UPDATE table_name SET field_name = field_name -1 WHERE field_name >
my_number


Note: if field_name is a unique field or key, you'll need to add an
ORDER BY clause to make sure this works.


But watch out if you're still using MySQL 3.x, as it doesn't support ordered
updates:

mysql> insert into test values (2), (3), (1);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> update test set id = id + 1;
ERROR 1062: Duplicate entry '3' for key 1
mysql> update test set id = id + 1 order by id;
ERROR 1064: You have an error in your SQL syntax near 'order by id' at line 1

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.