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; 

an sqlfiddle test with.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -