»
S
I
D
E
B
A
R
«
Using ORDER BY with UPDATE
July 21st, 2009 by admin

Yep, you read the title correctly. This problem presented itself when I needed a quick and dirty way to update an auto_increment [a_i] field in a database. I wanted to move the a_i field up by n, where a_i > x. The query looked like this:

UPDATE table_name SET id = (id + n) WHERE id > x ORDER BY id DESC

To see why one must do it this way, try to do the query without the SORT BY.

Nick


One Response

tmallare writes:
August 6th, 2009 at 6:48 pm

Knowledge is a great thing. Thank you for sharing yours.

Leave a Reply

You must be logged in to post a comment.

»  Substance: WordPress   »  Style: Ahren Ahimsa