r/mysql • u/CONteRTE • Sep 26 '24
question How to UPDATE a table with sequential numbering?
I have tried to update my data by a query like this:UPDATE table SET sorting = (row_number() over(order by sorting) - 1) where user = 1 and id <> 'myid' order by sorting;
But this failed. On the other handselect row_number() over(order by sorting) - 1 AS iterator, id, sorting from table where id = 1 and id <> 'myid' order by sorting
is working fine. How can i update the table the right way?
4
Upvotes
5
u/pskipw Sep 26 '24
X/y problem. Tell us why you need to do this and we’ll tell you how to do it correctly.
1
u/r3pr0b8 Sep 26 '24
why do you want to renumber the rows?
0
u/ssnoyes Sep 26 '24
One way is: