Chetan Chetan - 6 months ago 9
SQL Question

How to compare '>10' with 11 which is coming as parameter in mySql

This is the table i have.


And I need to select agains

Condition
column

select * from OfferCondition where Condition = '21'


for the above query, it should return 1st and 2nd row.

Answer

This requires a HAVING clause for each condition AND the conditions in order (so it stops at the first one... apart from that does as you request I think...

SET @iValue = 21;
SELECT substring_index(`Condition`,' ',1) AS Operator,
    substring_index(substring_index(`Condition`,' ',2 ),' ', -1) AS Value
FROM OfferCondition
HAVING (Operator = '>' AND @iValue > Value)
    OR (Operator = '>=' AND @iValue >= Value)
    OR (Operator = '=' AND @iValue = Value)
LIMIT 1;

NOTE this also requires a space between the operator and operand - as pointed out elsewhere would be much better to include as separate columns

Comments