Is there a way to do a + or - operation in a MySQL query?

4

Example:

select quantidade from tabela where quantidade é mais ou menos = '5';

Does anyone know of any way to do this in MySQL? I have an approximate value of 5 and would like to bring it from the database in a query, but I do not know any SQL function or any way to do the condition I want as in the example.

    
asked by anonymous 06.05.2016 / 14:47

3 answers

6

Using ABS is very simple:

SELECT campos FROM tabela WHERE ABS( valor - 5 ) < .5
                                             │      │
                          valor buscado 5 ───┘      │
  tolerancia 0,5 por exemplo (ajuste como quiser) ──┘

See working on SQL Fiddle .

The ABS( ) returns the value always positive, so what is after < is used to adjust the tolerance either more or less.

    
06.05.2016 / 19:12
5

The logic of "brown" can be made with an auxiliary variable of variation.

DECLARE @marromeno DECIMAL(0,2)
DECLARE @eixoDaGrampola DECIMAL(2,2)

-- atribua valores aqui

SELECT
    *
FROM
    inmetro
WHERE
    valor BETWEEN (@eixoDaGrampola - @marromeno) and (@eixoDaGrampola + @marromeno)

For example, if your variation is 0.2 for more or less ... If you give the values of 5 to @eixoDaGrampola " and 0.2 for @marromeno , you will get all values between 4.8 and 5.2. p>

Good luck.

    
06.05.2016 / 15:33
0

select quantity from table where quantity! = '5';

Resolve?

    
06.05.2016 / 18:54