I found this question ( How to get the max of two values in MySQL? ) while looking for ways to compare discrete set of values.
I want to be able to get the minimum of few selected values where some of them may be null
because they are optional but the MySQL docs says:
If any argument is NULL, the result is NULL.
Use a COALESCE()
function on the nullable values:
select LEAST(COALESCE(@foo, <max int>), COALESCE(@bar, <max int>));
if you get the max int value, then both were null (unless there's a decent chance you could actually have the max int value as a real argument, then more logic is necessary)
How about this:
LEAST(COALESCE(col1, col2, col3),
COALESCE(col2, col3, col1),
COALESCE(col3, col1, col2))
Obviously this doesn't scale well to more than 3 values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With