mysql - Auto increment null fields, leave non null fields as is -
i have table items
has field priority
. half of elements in field priority have unique value ranging 1
x
. want elements field priority
null have incremental values of x+1
y
.
i have tried
update items set priority = ifnull(priority, 0)
but not want. want non null fields stay is, achieved following:
set priority = ifnull(priority, 0)
and null fields start being incremented value, following:
select max(priority) items;
so thing comes mind close to
update items set priority = ifnull(priority, 0) + (select max(priority) items) priority null
however not sure how go doing so. hints or tips?
you can current maximum priority , store in variable. then, bring value query, using cross join
. can increment value in set
statement:
update items cross join (select @x := max(priority) items) maxp set priority = (@x := @x + 1) priority null;
Comments
Post a Comment