SQL问题
遇到以下陈述的问题
select *
from products
where Shop = 3
or Shop = 5
or Shop = 6
or Shop = 7
and Active <> 0
它选择商店没有问题,但总共绕过了查询的活动部分??
我错过了什么,但不确定是什么。 尝试移动语句,即首先是活动请求,但可能没有区别。
答案 0 :(得分:2)
喜欢这个
SELECT * FROM products WHERE shop IN (3,5,6,7) AND active <> 0`
OR
SELECT * FROM products WHERE ( shop = 3 OR shop = 5 OR shop = 6 OR shop = 7) AND active <> 0
答案 1 :(得分:1)
您需要在括号中包含OR语句:
select *
from products
where (Shop = 3
or Shop = 5
or Shop = 6
or Shop = 7)
and Active <> 0
否则有效&lt;&gt;使用Shop = 7
评估0答案 2 :(得分:1)
AND
之前评估 OR
SELECT * FROM products
WHERE (Shop = 3 OR Shop = 5 OR Shop = 6 OR Shop = 7)
AND Active <> 0
或
SELECT * FROM products
WHERE Shop IN (3,5,6,7)
AND Active <> 0
答案 3 :(得分:1)
您应该使用 IN 或()
SELECT * FROM products WHERE shop IN (3,5,6,7) AND active <> 0
OR
SELECT * FROM products WHERE ( shop = 3 OR shop = 5 OR shop = 6 OR shop = 7) AND active <> 0
答案 4 :(得分:0)
尝试使用括号,以便SQL引擎了解您的AND和OR条件:
select *
from products
where (Shop = 3
or Shop = 5
or Shop = 6
or Shop = 7 )
and Active <> 0
答案 5 :(得分:0)
and
优先级高于or
。所以Shop = 7并且有效&lt;&gt; 0一起计算。
所以下面的陈述是可以的。
SELECT * FROM products WHERE (Shop = 3 OR Shop = 5 OR Shop = 6 OR Shop = 7) AND Active <> 0
SELECT * FROM products WHERE Shop IN (3,5,6,7) AND Active <> 0