Simple Math max function in MySQL
How to find the maximum of two explicit values in MySQL? Something like MAXIMUM(1, @foo).
There are group functions like MAX, MIN, AVG, etc that take column name as an argument and wo开发者_开发问答rk with result sets. Is it possible to convert two explicit values to a result set and use those functions? Some other ways?
P.S.: I need a max function for one of my stored procedures.
How to find the maximum of two explicit values in MySQL? Something like MAXIMUM(1, @foo).
Use the GREATEST function:
GREATEST(1, @foo)
...will return whichever value is larger - if 1 is larger than the value in @foo, you'll get 1. Otherwise, you'll get whatever value is in @foo. Also, it's not an aggregate function.
The alternative would be to use a CASE statement:
CASE WHEN 1 > @foo THEN 1 ELSE @foo END
...because CASE is ANSI standard - that will work on Oracle, MySQL, SQL Server, Postgres...
You can use IF(1 > @foo,1,@foo)
精彩评论