There is a similar question Insert sequential number in MySQL
I want to insert sequential numbers to the table, but based on another field. I have two columns page_numner and parent, so the rows with same parent should have page_number as consequtive numbers. If parent changes, the page should start from 1 again and increase by one.
I was thinking to use smth like this
SELECT @i:=0;
SELECT @p:=0;
UPDATE my_table AS t SET page_number = CASE
WHEN @p = t.`parent` THEN @i:=@i+1
ELSE 1 -- assign current parent to @p ??
END
but, it cant figure out how to assign the new parent into @p for the else case.
Please note, that I am trying to achieve this with pure mysql (if possible of course)
Thanks