in reply to Increase a value inside MySQL query using perl

What I would do is this ... and no, I’m not going to write or describe the actual code for you, but rather just the idea:

(1)   Use placeholders for each query, such as SELECT primary_key FROM table WHERE column = ?.   Notice the question-mark, which is not enclosed in quotes.   That’s a placeholder.   You can now prepare your statement-handle and execute it many times, providing each time an array of values which will be substituted (left to right) for each placeholder in the prepared query.   This is more efficient and more secure.

Important Note:   If ID is the primary key ... well, first of all, changing the key is usually not a good idea (since the need to do so implies that the key’s value carries meaning, which is not a good thing to do with a database key) ... second, if you are incrementing the keys you must add ORDER BY ID DESC to your SELECT statement because you must increment the highest key-value first.   Your SELECT statement will not encounter the record a second time (once the record-set has been pulled, it remains unchanged), but you must avoid non-unique values in a primary key field.   If you do not specify the order in this (special ...) case, key-collisions will occur and the update will fail for that reason.   If you do need an ID field for some other reason, then define another (say, auto-increment ...) primary key known only to the database.

(2)   Prepare two queries:   a SELECT statement which finds all of the rows that you want to update, retrieving their primary key and the value you want to change, and a statement like UPDATE table SET column = ? WHERE primary_key = ?.   (Two placeholders.)

(3)   If possible, use SQL transactions.   You will begin the transaction before starting the loop, and “commit” after the loop ends.   In this way, either all of the updates happen, or (if you “rollback”) none of them do.   This is very important for maintaining consistency especially among other updates being made by others.   It is also more efficient.   Note that MySQL can do this with InnoDB but not MyISAM tables (iirc).

(4)   Now, to actually do the work, begin a transaction.   Now, execute the SELECT query and loop through the rows returned.   For each, execute the already-prepared UPDATE statement, supplying the primary-key and new value.   After the loop ends, commit.   Wrap the whole thing (after begin-transaction) in an eval block such that, if an exception is thrown, you can roll back the transaction, leaving your database just like it was before the program started.   (All of the UPDATEs happen, or none of them do.)

This is much stronger than simply updating values based on their original value, which can be very problematic because one update affects the next one.   Here, you are selecting (by key) the rows that you wish to update, then you are updating them (identifying them by key).   You are efficiently using placeholders to let you build and prepare the two queries once and then to execute (the UPDATEs) many times.   You are using transactions both for efficiency and to ensure that no other database user will see an incomplete set of changes.