Pergunta

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!

Foi útil?

Solução

Use the GREATEST() function:

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

Outras dicas

Use CASE

select case when your_column >= 0 
            then your_column
            else 1
       end
from your_table

or IF()

select if(your_column >= 0, your_column, 1)
from your_table
SELECT CASE WHEN <colname> < 1 THEN 1 ELSE <colname> END CASE

If it's less than one, it will return one. If it's larger than one, it will return the value that it is.

See more here:

http://dev.mysql.com/doc/refman/5.0/en/case.html

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top