Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL greater than or equal to operator is ignoring its or equal to obligation

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?

like image 451
bcmcfc Avatar asked Sep 08 '10 11:09

bcmcfc


People also ask

How do you do greater than or equal to in MySQL?

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.

How do you show not equal to in MySQL?

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 (! =).

What is the use of <> in MySQL?

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.

Which of the following MySQL comparison operators check if two values are not equal?

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.


1 Answers

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.

like image 161
Alex Avatar answered Oct 21 '22 22:10

Alex