Maison > Article > développement back-end > 一条sql话语中有两个between and
一条sql语句中有两个between and
想实现的功能:
按两个属性搜索,如图:
数据库设计如图:(数据库设计已定不能更改了)
这是category_value表
这是 buy_6表
我写的sql语句:
select b.* from category_value as v, buy_6 as b where status=3 and v.itemid = b.itemid and v.moduleid=6 and v.value BETWEEN 2 and 7 and v.oid=6 and v.value BETWEEN 1 and 10 and v.oid=4
如果只按一个数据搜索没问题,两个属性就搜索不出数据
sql中v.oid=4指的是颜色属性id为4,v.oid=6指的是高度属性id为6
------解决思路----------------------
这不是 between 的问题!
在你的 sql
select b.* from category_value as v, buy_6 as b where status=3 and v.itemid = b.itemid and v.moduleid=6 and v.value BETWEEN 2 and 7 and v.oid=6 and v.value BETWEEN 1 and 10 and v.oid=4
既然 v.oid 不可能同时等于 6 和 4
那么自然就不会有结果的
------解决思路----------------------
按你的意思,似乎应该是:
(v.value BETWEEN 2 and 7 and v.oid=6) or (v.value BETWEEN 1 and 10 and v.oid=4)
所以sql语句应该为:
select b.* from category_value as v, buy_6 as b where status=3 and v.itemid = b.itemid and v.moduleid=6 and ((v.value BETWEEN 2 and 7 and v.oid=6) or (v.value BETWEEN 1 and 10 and v.oid=4))