SELECT least value between two columns with MYSQL

Solal :

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.

GMB :

The way least() handles nulls is a documented behavior:

If any argument is NULL, the result is NULL. 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))

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=31659&siteId=1