The following will get the information needed for a new ref_number -- assuming there are no conflicts with existing ones:
select ref_number,
@rn := if(@refnum = ref_number, @rn + 1, 0) as seqnum,
@refnum = rev_number
from table t cross join
(select @rn := 0, @refnum := -1) const
order by ref_number;
You can put this into an update using join, assuming you have an id column:
update table toupdate join
(select @rn := 0, @refnum := -1, @prev_refnum := -1) const
set ref_number = ref_number +
(case when (@prev_refnum := @refnum) is null then NULL
when (@refnum := ref_number) is null then NULL
when ref_number := @prev_refnum then @rn := @rn + 1
else 0
end)
order by ref_number;
This is a rather complicated statement because MySQL does not make it easy to set variables in an update statement. The use of the case is simply to set the variables to remember the previous value. They are executed in order, even though they fail.