开发者

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 t开发者_如何学编程he 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 :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜