我在 MySQL 中有一个名为 sale 的表,其中有 3 列,如下所示: 销售:
id|customers|type
我想选择 type!=2 的销售或 type=2 且客户!=0 的 sales
我这样写声明:
sale.type != 2 OR (sale.type = 2 AND sale.customers != 0 )
但它没有给我正确的行。
此外,我必须对这个查询中的其他列使用 AND ,但所有列之间的运算符是 AND ,只是在这里我使用 OR 。
最佳答案
只要记住这条规则:将 OR 括起来!
where foo = 'bar'
and (sale.type != 2 OR (sale.type = 2 AND sale.customers != 0 ) )
and blah = 3
其实你的情况可以简化为:
( sale.type != 2 OR sale.customers != 0 )
因为逻辑上检查 sale.type = 2 是多余的。
关于MYSQL 语句一起 OR 和 AND,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15896574/