Biggest value from two or more fields

后端 未结 5 1057
不思量自难忘°
不思量自难忘° 2020-12-05 01:32

I need to get the biggest value from two fields:

SELECT MAX(field1), MAX(field2)

Now, how can I get biggest value from these two?

5条回答
  •  心在旅途
    2020-12-05 02:10

    In case you're selecting the GREATEST() for each row

    SELECT GREATEST(field1, field2)
    

    It will return NULL if one of the fields is NULL. You could use IFNULL to solve this

    SELECT GREATEST(IFNULL(field1, 0), IFNULL(field2, 0))
    

提交回复
热议问题