CREATE TABLE table_name (col_a double(10,2), col_b double(10,2), col_c double(10,2));
INSERT INTO table_name VALUES(36.3, 0, 6.3);
QUERY
SELECT FLOOR(36.3- 0 -6.3), FLOOR(col_a - col_b - col_c) AS calc, col_a, col_b, col_c
FROM table_name LIMIT 1;
RESULT
first selected value => FLOOR(36.3- 0 -6.3)
result in 30
.
second selected value => FLOOR(col_a - col_b - col_c)
which is equals to FLOOR(36.3- 0 -6.3)
result in 29
but i am expecting 30
Why these selects getting two different values?
This is a known problem in MySQL when using the
double
orfloat
type, which are not stored internally exactly as we see them.If you read the MySQL documentation, you will find a suggested workaround which is to use
decimal
instead ofdouble
. You can see in the following Fiddle that all is working as expected when usingdecimal(10,2)
as your column types:SQLFiddle