If a price in a row is 38.03
, then the following search restrictions should all return the row containg the result.
WHERE price >= '38.02' AND price <= '38.03'
(This works)
WHERE price >= '20' AND price <= '100'
(This works)
WHERE price >= '38.03' AND price <= '38.03'
(This doesn't work)
WHERE price >= '38.03' AND price <= '100'
(This doesn't work)
WHERE price >= '38.03'
(This doesn't work)
WHERE price <= '38.03'
(This works)
The price is stored as a float in the DB.
So basically, <=
is working whereas >=
is not. Is there a reason why that could be?
In MySQL, you can use the >= operator to test for an expression greater than or equal to. SELECT * FROM contacts WHERE contact_id >= 50; In this example, the SELECT statement would return all rows from the contacts table where the contact_id is greater than or equal to 50.
MySQL Not Equal is an inequality operator that used for returning a set of rows after comparing two expressions that are not equal. The MySQL contains two types of Not Equal operator, which are (< >) and (! =).
The symbol <> in MySQL is same as not equal to operator (!=). Both gives the result in boolean or tinyint(1). If the condition becomes true, then the result will be 1 otherwise 0.
The SQL Not Equal comparison operator (!=) is used to compare two expressions. For example, 15 != 17 comparison operation uses SQL Not Equal operator (!=) between two expressions 15 and 17.
keep in mind that float
is a flawed data type when it comes to precision. If you represent 12
as float, you will get 11.99999999999998
or something.
'38.03'
can be converted to decimal, or other data type that is more precise (depending on RDBMS, I am being general here), and it will differ from the float value.
float is 32 bit, low precision. Double works a lot better, being 64 bit data type. Decimal data type in some systems are 128 bit numeric data types for storing very precise numeric values, and is usually used for denominating money.
And, skip the habit of comparing using the =
operator, of float
values. Floats are used for approximate and fast calculations, and only comparison with a range is acceptable for checking the value of a float
. That's valid for basically every single system.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With