I need to write a simple query in mySQL that would use MIN() in WHERE clause, calculating a minimum value of two numbers, where one of them is column value (with possible NULL value) and other one is a fixed number, something like this >
SELECT ... WHERE ... (len <= MIN(maxStay, 365))
I know I can write something like >
SELECT ... WHERE ... ((len <= maxStay) OR (maxStay IS NULL)) AND (len <= 365)
but I would prefer something simple, such as using MIN() in WHERE clause. Is it possible? And if so, which has better performance?
To return the lesser of the two values, use the
LEASTfunction in MySql. To handle the null values, useIFNULL().