1

How can I auto increment id column based on the 0 to max (int) value on the price column.

Lets assume that price is ASC, I want the id = 1 increment to start at the $0 mark. What would I have to edit for this code ALTER TABLE table AUTO_INCREMENT = 1 ?

id | price | other columns 
1  | 9
2  | 1
3  | 2
4  | 5

into

id | price | other columns
1  | 1
2  | 2
3  | 5
4  | 9
5
  • Sorry, I can not understand your question. Could you please explain it better? Commented Aug 24, 2015 at 17:29
  • it seems already answered here stackoverflow.com/questions/10242311/… Commented Aug 24, 2015 at 17:32
  • This is not how auto increment works by definition. Commented Aug 24, 2015 at 17:32
  • I want to make auto increment on ID column based on the 0 to max price column Commented Aug 24, 2015 at 18:06
  • You can't and you shouldn't do that, that isn't what auto_increment is supposed to do. You can search for stupid answers that tell you how to, but in a few days you'll be asking a whole different array of questions that will spawn from misuse of auto_increment. It's job is not to hand out pretty sequential numbers. If you understand that, then you can utilize triggers and create another column to do nice numbering based on some condition. Commented Aug 24, 2015 at 19:28

4 Answers 4

1

sorry, you must send 2 Querys. The first to set rhe Variable nr to 0 and then the update statement

SET @nr:=0;
UPDATE your_table_name t
  SET t.id = @nr:=@nr+1
  ORDER BY t.price ASC;
Sign up to request clarification or add additional context in comments.

2 Comments

You do realize you can edit an answer you posted and update it? What's the point of 4 wrong answers here?
maybe I am doing something wrong but it does not seem to work. It takes a really long time and does not finish.
0

A easy Way ist to create a new table with one column more with autoincrement.

intert into newtable select '',o.* from oldtable o order by o.price asc;

Then they insert all row in the right order from price in the new table.

3 Comments

I don't want to make a new table every time just for this
then the row number every time changed when you sort the result by price with new rows.
has to be a way to just update the current id column based on the price column
0

if you only want to have a row with rownumber, so you can do it like this:

SELECT @nr:=@nr+1 AS id,c.* FROM mytable c, (SELECT @nr:=0) tmp;

1 Comment

if I create a new column f_id how would I be able to auto increment the new column based on the price asc column? And if this is possible would I be able to delete old f_id column and create a new f_id column with auto increment whenever I update the db?
0

the feld x is NOT autoincrement. its only a non uniqe index

SET @nr:=0;
UPDATE newtable n SET n.x = @nr:=@nr+1 ORDER BY n.price ASC;

1 Comment

sorry I'm a little new. not sure how I would use this approach

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.