Chetan Chetan - 1 year ago 70
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


select * from OfferCondition where Condition = '21'

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

Answer Source

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)

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download