mysql - SQL sort column A & update column B -
i have table data similar to:
inven descript printorder --------------------------------- 1 d 9 2 b 0 3 5 4 z 0 5 x 1 . . . . . . . . .
i sort table on column descript descending alpha (a - z) , update column printorder when done, record printorder = 1 the highest alpha (a) , record highest value printorder lowest in alpha (z).
is possible without using temporary columns? not deal breaker if not, preference.
desired result:
to update printorder values based on sorting result
inven descript printorder --------------------------------- 1 d 3 2 b 2 3 1 4 z 5 5 x 4
i unclear whether want modify table or create result set. here solution latter using standard sql:
select inven, descript, row_number() on (order descript) printorder table t order descript;
edit:
in mysql, select
like:
select t.* (select inven, descript, (@rn := @rn + 1) printorder table t cross join (select @rn := 0) params order descript ) t order inven;
the update
trickier, because cannot sort and initialize variable:
update t set printorder = (@rn := coalesce(@rn + 1, 1)) order descript;
Comments
Post a Comment