Using ORDER BY with UPDATE

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 ORDER BY.

Nick

One thought on “Using ORDER BY with UPDATE

Leave a Reply