Thursday, 3 May 2018

How to get the max of two values in MySQL?

You can use GREATEST function with not nullable fields.
If one of this values (or both) can be NULL, don't use it (result can be NULL).



select 
if(
fieldA is NULL,
if(fieldB is NULL, NULL, fieldB), /* second NULL is default value */
if(fieldB is NULL, field A, GREATEST(fieldA, fieldB))
) as maxValue



You can change NULL to your preferred default value (if both values is NULL).

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...