Search code examples
mysqlselect-query

Mysql select query to find a value in certain range


I have a items table,

id    item    min_price   max_price
-----------------------------------
1     item1    100         500
2     item2    150         400
3     item3    410         700
4     item4    330         700
5     item3    420         600

When I pass the price value 450, I expect a result with 450 containing range values, ie,

 id    item    min_price   max_price
-----------------------------------

3     item3    410         700
5     item3    420         600

How do I get this result ?

I tried this query,

SELECT * FROM items where min_price >= 450 AND max_price <= 450

But no result. How to get the correct result ?


Solution

  • You mixed them up:

    SELECT * FROM items 
    WHERE min_price <= 450 
      AND max_price >= 450
    

    You can always use between which will help you to avoid this problems in the future:

    SELECT * FROM items 
    WHERE 450 between min_price and max_price