多列价格范围sql查询

时间:2015-11-03 05:21:56

标签: sql sql-server range multiple-columns

无法解决此问题:

想要搜索具有类别,子类别,更低价格范围和更高价格范围的产品

select * from product p join category c 
ON(p.category_id=c.category_id) where p.category=? and p.sub_category_id=?
and lower_price_limit between ? and ? or upper_price_limit between ? and ?

例如:产品范围从200 to 250到通过220 to 230这不起作用。

由于

3 个答案:

答案 0 :(得分:1)

您忘记在WHERE子句中添加括号:

SELECT * 
FROM product p 
JOIN category c 
    ON p.category_id = c.category_id
WHERE
    p.category = ? 
    AND p.sub_category_id = ? 
    AND (
        lower_price_limit BETWEEN ? AND ? 
        OR upper_price_limit BETWEEN ? AND ?
    )

答案 1 :(得分:0)

尝试此查询: -

 select * from product p join category c 
    ON p.category_id=c.category_id where p.category=? and p.sub_category_id=?
    and (lower_price_limit between ? and ? or upper_price_limit between ? and ?)

答案 2 :(得分:0)

select * 
from product p 
join category c ON p.category_id=c.category_id 
where p.category=? and p.sub_category_id=? and 
      (lower_price_limit between ? and ? or upper_price_limit between ? and ?)