Search code examples
mysqlmysqlimport

Is there a function to get a minimum or the Value in Mysql?


I want to select all values from a table. One column is a SIGNED INTEGER that also contains negative numbers. I want set a minimum for this column (1) while selecting. Like this:

If the value is 20 i want to get 20.

If the value is 1, I want to get 1.

But when the value smaller than 1 is, then I want only to get 1.

Is there a function that allows me to do this? It should only be 1 command because i want to use it in PHP with mysqli!


Solution

  • Use the GREATEST() function:

    SELECT GREATEST(-1,1); -> 1
    SELECT GREATEST(20,1); -> 20