Hello I am trying to select the smallest value between 3 columns with MySQL.
The issue I am facing is that in a column I might have NULL
value. How can I get the least value that is not NULL
?
Running select least(1, 3, NULL)
will return NULL
but I would like to have 1
as an answer.
Thank you for your help.
The way least()
handles null
s is a documented behavior :
If any argument is
NULL
, the result isNULL
. No comparison is needed.
That might seem annoying, but propagating the null
value to the resultset is how your database signals you that one of your values is undefined; this is consistent with the behavior of other operations that involve null
(string concatenation, arithmetic operations, ...).
If only the third column may be null
, and none of the other two, you could do:
least(col1, col2, coalesce(col3, col1))
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.