简体   繁体   中英

max(x,y) in MySQL

For the following table (all columns are integers)

[id, value, best_value]

For a given id and value I want update it's row setting the best_value column to max(newvalue,best_value). I seached into the documentation but I dont see a function for doing so.

Thanks

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

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

You don't strictly need any such function,

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

would do the job about as well as AlienWebguy's answer :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM