1

I have table with position atribute 'posit' with unknown values (in my example '0') and I want to UPDATE it to 1,2,3, ...

BEFORE:

 _______________
| title | posit |
|---------------|
|  test |   0   |
|-------|-------|
|  test |   0   |
|-------|-------|
|  test |   0   |
|-------|-------|
|  test |   0   |
'---------------'

AFTER:

 _______________
| title | posit |
|---------------|
|  test |   1   |
|-------|-------|
|  test |   2   |
|-------|-------|
|  test |   3   |
|-------|-------|
|  test |   4   |
'---------------'

Something like this

UPDATE myTable 
SET posit = last_updated_value() + 1 
WHERE title='test';

Is there any way to do it by SQL command? Note that 'posit' is not auto increment. I have only PHP solution.

Thanks Henry

1 Answer 1

3

You have mysql as a tag, so with that you could use a user defined variable. Something like this:

SET @incr = 0;
SELECT @incr:=@incr+1 FROM DUAL;

See http://dev.mysql.com/doc/refman/5.0/en/user-variables.html for more details.

Sign up to request clarification or add additional context in comments.

3 Comments

That will only return one row, with a value of one.
It was meant as a sample, not a definitive solution. The next time you run the select, it will give you back a 2.
Thanks. You open my eyes. So this is final SQL. SET @incr = 0; UPDATE myTable SET posit = @incr:=@incr+1 WHERE title='test';

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.