MySQL - Update column with MAX or GREATEST Value from fields in row (excluding NULL) -
in mysql in table mytable
want update column max_value
example before query table looks like:
___________________________________________________ || id || value1 || value2 || value3 || max_value || ___________________________________________________ || 1 || 1 || 2 || 3 || null || =================================================== || 2 || 99 || 2 || 3 || null || =================================================== || 3 || 1 || 66 || 66 || null || =================================================== || 4 || 1 || 2 || null || null || =================================================== || 5 || null || null || null || null || ___________________________________________________
ater sql statement max_value
should have been updated highest value of value1
, value2
or value3
. this:
___________________________________________________ || id || value1 || value2 || value3 || max_value || ___________________________________________________ || 1 || 1 || 2 || 3 || 3 || =================================================== || 2 || 99 || 2 || 3 || 99 || =================================================== || 3 || 1 || 66 || 66 || 66 || =================================================== || 4 || 1 || 2 || null || 2 || =================================================== || 5 || null || null || null || null || ___________________________________________________
can give me mysql query this?
greatest
doesn't interact null
in way need, update
/join
without too complexity;
update table1 t1 join ( select id,max(value) value ( select id,value1 value table1 union select id,value2 table1 union select id,value3 table1 ) g group id ) on t1.id = a.id set t1.max_value=a.value;
Comments
Post a Comment