Если вы используете MysQL 8.0, вы можете просто использовать row_number()
для этого:
select
learning_path,
course,
row_number() over(partition by learning_path order by course) position
from mytable
Если вам нужен запрос update
:
update mytable t
inner join (
select
learning_path,
course,
row_number() over(partition by learning_path order by course) position
from mytable
) t1 on t1.learning_path = t.learning_path and t1.course = t.course
set t.position = t1.position
В более ранние версии, один из вариантов:
update mytable t
inner join (
select
learning_path,
course,
(select 1 + count(*) from mytable t1 where t1.learning_path = t.learning_path and t1.course < t.course) position
from mytable t
) t1 on t1.learning_path = t.learning_path and t1.course = t.course
set t.position = t1.position