Mysql select query to find a value in certain range

时光怂恿深爱的人放手 提交于 2020-01-30 11:40:27

问题


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 ?


回答1:


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


来源:https://stackoverflow.com/questions/36306512/mysql-select-query-to-find-a-value-in-certain-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!