Your request is great for me. I tried to launch it from MySQL Query Browser:
CREATE TABLE tbl (Id INT NOT NULL, SortId INT NOT NULL); INSERT INTO tbl (Id, SortId) VALUES (1, 9), (2, 22), (3, 13); SET @a:=0; UPDATE tbl SET sortId=@a : =@a +1 ORDER BY sortId; SELECT * From tbl;
Result:
Id sortId 1 1 2 3 3 2
Please note that when executing queries from MySQL Query Browser, one query per row should be entered, and not two on one row, as you do. If you want to put this in a stored procedure (maybe this is a good idea), you can create it like this:
DELIMITER // CREATE PROCEDURE updateSortIds() BEGIN SET @a:=0; UPDATE tbl SET SortId=@a : =@a +1 ORDER BY SortId; END // DELIMITER ;
And to execute it, use this:
CALL updateSortIds();
Mark byers
source share