MySQL 5.5.32, Oracle 11g
MySQL returns strange results for queries involving mod()
with noninteger moduli. Why is this?
I have a column defined thus:
+--------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| col | double(15,6) | YES | | NULL | |
+--------+--------------+------+-----+---------+-------+
I have selected a known value from that column to use for testing - 5850. If I query the table, testing that column for whole numbers, I get these results:
mysql> SELECT thing_id,name FROM table WHERE col=5850 AND mod(col,1) != 0;
Empty set (1.07 sec)
This is expected. But this is not:
mysql> SELECT thing_id,name FROM table WHERE col=5850 AND mod(col,.1) != 0;
+-----------+-----------+
| thing_id | name |
+-----------+-----------+
| 4444444 | some |
| 4444445 | names |
...
| 55555555 | go |
| 55555556 | here |
+-----------+-----------+
416 rows in set (1.07 sec)
The same results from substituting col MOD .1
and col % .1
so it has to do with the computation not the semantics.
Compare this with the behavior of Oracle (using SQL Developer if that matters), where the type of col
is NUMBER
:
SELECT thing_id,name FROM table WHERE col=5850 AND mod(col,.1) != 0;
thing_id name
[NO RECORDS]
The precision on the MySQL column seems to make this difference, but I don't understand how. Conceptually, shouldn't integer_value % .1 = 0
?