View Full Version : [SOLVED] SQL Update entry +1

09-16-2015, 02:21 PM
Hey guys, i would like to know how to update the entry +1, and loop it till the last row.
So for example looping it; http://prntscr.com/8gzv5w

I would like to update the rows to 500001 + 1 and loop it, so it updates each row,
for example
500001, 500002, 500003, 500004, 500005,

09-16-2015, 02:58 PM
Depending on what language you're using with the SQL query.. You would probably use a for loop

or else you can just update individually on one query eg: UPDATE table SET blah=value WHERE this=value, SET blah=value WHERE this=value2, SET blah=value, WHERE this=value3

09-16-2015, 03:24 PM
UPDATE item_template set entry = ((SELECT selected_value FROM (SELECT MAX(entry) AS selected_value FROM item_template) AS sub_selected_value) + 1) WHERE entry = 1;

Thanks to this site here: http://the-stickman.com/uncategorized/mysql-update-with-select-on-the-same-table-in-the-same-query/ Basically this will update all records with the entry 1 to the max entry value using a sub query.

09-16-2015, 04:54 PM
Neccta unfortunately that did not work, but yes that is what i am looking for hehe. Faded i'm trying to update the entry row to start at 500.001 and then increase by 1 for all the rows in the table.
Doing it manually would take way to much time.

Set @entry 500001
Update entry + 1;

Wouldn't that work?

09-16-2015, 06:53 PM
This should do the trick, this query will update the entry column of every record in the item template table.
The first record will have the entry of '@start_value' and it will then keep incrementing.

So basically what you said:

Set @entry 500001
Update entry + 1;

SET @START_VALUE = 500000;

UPDATE item_template SET entry = @START_VALUE := @START_VALUE+1;

09-17-2015, 07:26 AM
Thank you this is what i needed.