简体   繁体   中英

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM