Auxiliary
Auxiliary

Reputation: 2757

MySQL - Use min function or limit an order-by query

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

Answers (2)

Iłya Bursov
Iłya Bursov

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

juergen d
juergen d

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

Related Questions