Increment a counter per row in update command in MYSQL 4

I need to set the value of sortkey to the value of num, plus an increasing counter, for all rows in a table.  The attached code is how I'd EXPECT to run it, but it fails.  I'm running MySQL 4.1.22.

Any suggestions would be welcome.  I tried ROW_COUNT, but that isn't in MySQL 4.  Neither are stored procedures.  I'd like to keep this as an SQL statement so that I don't need to extract the data and process in PHP or Perl.
SET @count=1;
UPDATE testtable SET sortkey=concat(num,@count), @count=@count+1 WHERE num = 10;
 
SET @count=1;
UPDATE testtable SET sortkey=concat(num,@count), @count=@count+1 WHERE num = 20;
 
+----+-----+---------+
| id | num | sortkey |
+----+-----+---------+
|  1 |  10 |     101 |
|  2 |  20 |     201 |
|  3 |  10 |     102 |
|  4 |  10 |     103 |
|  5 |  10 |     104 |
|  6 |  20 |     202 |
+----+-----+---------+

Open in new window

LVL 1
oreomikeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oreomikeAuthor Commented:
I guess I need to mention that I need to do this for all rows in the table, but the count needs to be separate per 'num'.  If there is a way to define a hash to store counts, that may be better.

Here is my re-written attempted code, and desired table values.  Sortkey is currently NULL for all rows.
SET @count=1;
UPDATE testtable SET sortkey=concat(num,@count), @count=@count+1 
WHERE num = 10;
 
SET @count=1;
UPDATE testtable SET sortkey=concat(num,@count), @count=@count+1 
WHERE num = 20;
 
+----+-----+---------+
| id | num | sortkey |
+----+-----+---------+
|  1 |  10 |     101 |
|  2 |  20 |     201 |
|  3 |  10 |     102 |
|  4 |  10 |     103 |
|  5 |  10 |     104 |
|  6 |  20 |     202 |
+----+-----+---------+

Open in new window

Roger BaklundCommented:
Try this:
SET @count=0;
UPDATE testtable SET sortkey=concat(num,@count:=@count+1)
WHERE num = 10;
 
SET @count=0;
UPDATE testtable SET sortkey=concat(num,@count:=@count+1)
WHERE num = 20;

Open in new window

oreomikeAuthor Commented:
no, that doesn't work
SolarWinds® Network Configuration Manager (NCM)

SolarWinds® Network Configuration Manager brings structure and peace of mind to configuration management. Bulk config deployment, automatic backups, change detection, vulnerability assessments, and config change templates reduce the time needed for repetitive tasks.

Roger BaklundCommented:
It works on my server, see below. If it does not work for you, please explain in what way it does not work: do you get an error message, do you get different results, or what?
mysql> select * from testtable;
+------+------+---------+
| id   | num  | sortkey |
+------+------+---------+
|    1 |   10 |    NULL |
|    2 |   20 |    NULL |
|    3 |   10 |    NULL |
|    4 |   10 |    NULL |
|    5 |   10 |    NULL |
|    6 |   20 |    NULL |
+------+------+---------+
6 rows in set (0.01 sec)
 
mysql> SET @count=0;
Query OK, 0 rows affected (0.04 sec)
 
mysql> UPDATE testtable SET sortkey=concat(num,@count:=@count+1)
    -> WHERE num = 10;
Query OK, 4 rows affected (0.10 sec)
Rows matched: 4  Changed: 4  Warnings: 0
 
mysql>
mysql> SET @count=0;
Query OK, 0 rows affected (0.00 sec)
 
mysql> UPDATE testtable SET sortkey=concat(num,@count:=@count+1)
    -> WHERE num = 20;
Query OK, 2 rows affected (0.09 sec)
Rows matched: 2  Changed: 2  Warnings: 0
 
mysql> select * from testtable;
+------+------+---------+
| id   | num  | sortkey |
+------+------+---------+
|    1 |   10 |     101 |
|    2 |   20 |     201 |
|    3 |   10 |     102 |
|    4 |   10 |     103 |
|    5 |   10 |     104 |
|    6 |   20 |     202 |
+------+------+---------+
6 rows in set (0.00 sec)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
oreomikeAuthor Commented:
I get an error message, invalid SQL, at the @count:=@count+1 part of the statement.

What version of mysql server and version are you using?  I'm using MySQL 4.1.22.  I know there is additional things like this that were added to MySQL 5.
Roger BaklundCommented:
Strange. This should also work in version 4.1, even in 4.0 and 3.23.

http://dev.mysql.com/doc/refman/4.1/en/user-variables.html

Are you running the test statements above, or are you using this in a different statement? If it is a different statement, show the query you are using and the exact error message.
oreomikeAuthor Commented:
My mistake, my command was different than I stated in the question.
oreomikeAuthor Commented:
In the question, I was setting a field to the value of the user variable separately from the update of the user variable.

What I needed to do as cxr pointed out, was to increment the variable at the same time it is assigned to the column.

See code below
mysql> update testm set sortkey=@count:=@count+1 where sortkey=0;
 
Query OK, 100 rows affected (0.01 sec)
Rows matched: 100  Changed: 100  Warnings: 0
 
 
mysql> update testm set sortkey=@count, @count:=@count+1 where sortkey=0;
 
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@count:=@count+1 where sortkey=0' at line 1

Open in new window

IceColdasCommented:
UPDATE testm SET sortkey = (SELECT @rownum:=@rownum+1 AS newpos FROM  (SELECT @rownum:=0) r )
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.