/*mysql中假如以parent_code为示例来按编号来重新生成序号*/
select
---rownum 判断 @pre_parent_code是否和当前的parent_code一样 ,true:让 @i+=1 false:重置@i
(@i := case when @pre_parent_code=parent_code then @i + 1 else 1 end ) rownum,
b.parent_code,
b.id,
(@pre_parent_code:=parent_code) as 变量 /*要保留上面判断调用的*/
from my_test b ,
(SELECT @i := 0, @pre_parent_code:='') AS a
order by parent_code
/*最后生成的结果。*/