I need to select all products whose fields conform to a simple mathematical expression (in my case, division).
Example Query:
SELECT * FROM cars WHERE (retail_price/invoice_price)>1.05
Needs to be flexible:
I need flexibility here because I wish to be able to define the expression as a string somewhere else and just pop it in the SQL query after the WHERE clause.
$expression = "(retail_price/invoice_price)>1.05";
$query = "SELECT * FROM cars WHERE ".$expression." LIMIT 1";
Notes:
– Data type of retail_price is VARCHAR
– Data type of invoice_price is VARCHAR
– Not sure if VARCHAR is suitable for monetary values though..
Any suggestions? Because the above did not work for me. (Returned zero results)
It could be a rounding problem (as @Justin said).
Try this:
The idea is to force engine to manage floats (multiplying for float number 1.0).
UPDATE:
I see you’ve just edited data type to VARCHAR.
Anyway the result (for me) is the same: look at http://sqlfiddle.com/#!2/298d3/2