Is it possible to do a + or – operation on the value in a MySQL query?

Question:

Example:

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

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

Answer:

Using ABS , it's very simple:

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

See working in SQL Fiddle .

The ABS( ) always returns a positive value, so the one after the < serves to adjust the tolerance either up or down.

Scroll to Top