Max (x, y) in MySQL

In the following table (all columns are integer)

[id, value, best_value] 

For this id and value, I want it to update the row by setting the best_value column to max (newvalue, best_value). I connected to the documentation, but I do not see the function for this.

thanks

+4
source share
2 answers

You want GREATEST(x,y) . For example, if the new value is 530:

UPDATE my_table SET best_value = GREATEST(530,best_value) WHERE id=123

+4
source

You do not need such a function,

 UPDATE my_table SET best_value = new_value WHERE id=123 AND best_value < new_value 

will accomplish this task as well as AlienWebguy's answer :)

+3
source

All Articles