Web Hosting Talk







View Full Version : Mysql query to increment rows by 10+?


lexington
12-14-2009, 01:48 PM
Hello, I just added a new row in my DB table that starts at 10. Can I run a query that will add 10 to each row so that it will display

new_row = 10
new_row = 20
new_row = 30

and so on automatically? Basically each entry will have 10+ added to this new field. Thanks.

larry2148
12-14-2009, 02:00 PM
You'll have to run these commands from your mysql command prompt:

SET @@auto_increment_offset=10; will start you off at 10
SET @@auto_increment_increment=10; will change the increment from there on to 10,giving you the results you want.

You may need to recreate your table(or at least dump all the data out and readd it).

lexington
12-14-2009, 02:10 PM
Thanks, but the field is not an increment field. I already have an auto increment field and want to keep it that way, however I have a second field that I would like to do this to. Basically I am adding a new arrangement feature, and as you may know you have to assign values to it so that users can arrange the order of the list the way they want. If they all have the same value, if one listing is moved, it will jump way up or down the list. So I need to make every item in the list incremented for this new field, and not the auto_increment field.

If it is not possible with mysql I may create a little php script perhaps that will just add 10+ to every row (once I figure out how to do that).

lexington
12-14-2009, 05:23 PM
Nevermind, I created a php script that does this. It also resets the counter per "group" so it is perfect :)

larry2148
12-14-2009, 05:29 PM
Ah ok,

In that case I would think that you need to make that move with the script that creates the record, it could find the last value and simply add 10.

Another thing you may look into is using the auto_increment anyway, I'm thinking you can let it auto_increment because it will continue from where it left off regardless of what the rows have been reorganized/deleted as.

For example if you have an auto_increment column with rows 1-10 and you remove all those records, it will still start at 11 next time.

larry2148
12-14-2009, 05:29 PM
Cool good to know!