Reputation: 147
I am not able to select from my adSpec table where the width value matches exactly, for a specific value. This does not happen for all cells within the column.
My adSpec Table
╔═══════════╦══════════════╗
║ adSpecId ║ width ║
╠═══════════╬══════════════╣
║ 100 ║ 154.00800 ║
║ 101 ║ 319.96799 ║
║ 102 ║ 342.00000 ║
║ 103 ║ 342.00000 ║
╚═══════════╩══════════════╝
Does NOT return result
SELECT * FROM `adSpec` where `width` = '154.00800'
DOES return result
SELECT * FROM `adSpec` where `width` LIKE '154.00800'
Using "=" even works with other values in the same column
SELECT * FROM `adSpec` where `width` = '319.96799'
Note: There are no trailing spaces after the value within the db
Width Column Details
Type: float
Length: 10
Decimals: 5
Upvotes: 0
Views: 69
Reputation: 6065
FLOAT is not reliable for precise comparison.
Because floating-point values are approximate and not stored as exact values, attempts to treat them as exact in comparisons may lead to problems. They are also subject to platform or implementation dependencies. For more information, see Section B.5.4.8, “Problems with Floating-Point Values” https://dev.mysql.com/doc/refman/5.7/en/floating-point-types.html
Here is a demo on the unreliability:
mysql> create table adSpec(adSpecId int, width float(10,5));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into adSpec values(100,154.00800);
select *,Query OK, 1 row affected (0.01 sec)
mysql> select *, round(width, 20), width = '154.00800' from adSpec;
+----------+-----------+--------------------------+---------------------+
| adSpecId | width | round(width, 20) | width = '154.00800' |
+----------+-----------+--------------------------+---------------------+
| 100 | 154.00800 | 154.00799560546875000000 | 0 |
+----------+-----------+--------------------------+---------------------+
1 row in set (0.00 sec)
If you want to do exact comparison, try decimal.
DECIMAL:
mysql> create table adSpec(adSpecId int, width decimal(10,5));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into adSpec values(100,154.00800);
Query OK, 1 row affected (0.00 sec)
mysql> select *, round(width, 20), width = '154.00800' from adSpec;
+----------+-----------+--------------------------+---------------------+
| adSpecId | width | round(width, 20) | width = '154.00800' |
+----------+-----------+--------------------------+---------------------+
| 100 | 154.00800 | 154.00800000000000000000 | 1 |
+----------+-----------+--------------------------+---------------------+
1 row in set (0.00 sec)
Upvotes: 3
Reputation: 261
A decimal(10,3) will have only 3 decimal places and can be compared in the same manner as integers.
http://dev.mysql.com/doc/refman/5.1/en/precision-math-decimal-changes.html
Upvotes: 0
Reputation: 749
Upvotes: 0