Today, working on the site, I had to separate the main directory from the additional one. And in the additional catalog it was necessary to number the necessary entries in the form “Project 1”, “Project 2”. And then some unknown beast did not allow me to do it quickly in any widely used programming language. I wanted to try, and can I do it using only MySQL tools?
As I recall, there are variables in MySQL, for example @a. But searching the net for how to do a loop in MySQL did not give me anything.
Then I thought, because we can write
SELECT @i:=@i+1;
And UPDATE in turn passes each record and replaces the value one by one.
SELECT @i := 0; UPDATE `table` SET `name`=CONCAT(' ', @i := @i+1) WHERE `type` = 1 ORDER BY `id`;
As a result, we renamed the records with type 1 in the order of their ID.
PS: CONCAT concatenates strings.