Reputation: 2757
I am writing a query to find the product with the minimum price. These are the two queries I tried:
select min(price) from products
and
select price from products order by price limit 1
The first one returns 19.950000762939453 and the second one returns 19.95 which is the accurate value. So my question is, what's the difference of the two queries?, why is the first one weird?! and which has a better performance for this task?
Thanks in advance.
Upvotes: 0
Views: 123
Reputation: 24156
min has better performance, according strange values - you should read how floating numbers are stored in memory/db, they are "rounded"
if you store real price - go with DECIMAL type, it will work fine
Upvotes: 1
Reputation: 204884
Your data type of price
is probably a floating-point with is by definition inaccurate.
If you use a fixed-point data type like decimal
it will be 19.95
.
You can read it up in the doc
Upvotes: 2