具有多个OR条件和单个AND条件的MySQL查询

时间:2016-01-13 23:50:18

标签: mysql

我有一个MySQQL数据库表,我根据一些用户输入动态地使用PHP构建SQL语句。

SQL最终看起来像这样....

SELECT * 
FROM  `nam_order_items` 
WHERE item_status =  'Glass Bending - Andy'
OR item_status =  'Glass Bending - Bren'
OR item_status =  'Glass Bending - Gary'
OR item_status =  'Glass Bending - James'
OR item_status =  'Glass Bending - Oscar'
AND (
`backing_cut` =0
)

基本上,DB列item_status可以是其中任何一列,backing_cut列可以是0或1。

使用上面的SQL,它会返回与item_status值匹配的所有记录,而不管backing_cut值是什么。

它应该允许我根据backing_cut为0或1的值来过滤掉结果中的记录。

我做错了什么?

除了上面的SQL,我还试过....

SELECT * 
FROM  `nam_order_items` 
WHERE item_status =  'Glass Bending - Andy'
OR item_status =  'Glass Bending - Bren'
OR item_status =  'Glass Bending - Gary'
OR item_status =  'Glass Bending - James'
OR item_status =  'Glass Bending - Oscar'
AND `backing_cut` =0

SELECT * 
FROM  `nam_order_items` 
WHERE item_status =  'Glass Bending - Andy'
OR item_status =  'Glass Bending - Bren'
OR item_status =  'Glass Bending - Gary'
OR item_status =  'Glass Bending - James'
OR item_status =  'Glass Bending - Oscar'
(
    AND `backing_cut` =0
)

3 个答案:

答案 0 :(得分:2)

使用括号

SELECT * 
FROM  `nam_order_items` 
WHERE (item_status =  'Glass Bending - Andy'
OR item_status =  'Glass Bending - Bren'
OR item_status =  'Glass Bending - Gary'
OR item_status =  'Glass Bending - James'
OR item_status =  'Glass Bending - Oscar')
AND (`backing_cut` = 0 OR `backing_cut` = 1 OR `backing_bent` = 0 OR `backing_bent` = 1)

您也可以将数组中的值用作

SELECT * 
FROM  `nam_order_items` 
WHERE `item_status` IN  ('Glass Bending - Andy','Glass Bending - Bren','Glass Bending - Gary','Glass Bending - James','Glass Bending - Oscar')
AND (`backing_cut` = 0 OR `backing_cut` = 1 OR `backing_bent` = 0 OR `backing_bent` = 1)

然后您可以使用php构建您的数组,并执行以下操作:

$options = array('Glass Bending - Andy','Glass Bending - Bren','Glass Bending - Gary','Glass Bending - James','Glass Bending - Oscar');

$sql = "SELECT * FROM `nam_order_items` WHERE `item_status` IN ({implode(',', $options}) AND (`backing_cut` = 0 OR `backing_cut` = 1 OR `backing_bent` = 0 OR `backing_bent` = 1)";

编辑添加支持cut = 1或0

的选项

答案 1 :(得分:1)

连词优先于分离,所以

SELECT * 
FROM  `nam_order_items` 
WHERE item_status =  'Glass Bending - Andy'
OR item_status =  'Glass Bending - Bren'
OR item_status =  'Glass Bending - Gary'
OR item_status =  'Glass Bending - James'
OR item_status =  'Glass Bending - Oscar'
AND `backing_cut` =0

SELECT * 
FROM  `nam_order_items` 
WHERE item_status =  'Glass Bending - Andy'
OR item_status =  'Glass Bending - Bren'
OR item_status =  'Glass Bending - Gary'
OR item_status =  'Glass Bending - James'
OR item_status =  'Glass Bending - Oscar'
(
    AND `backing_cut` =0
)

在语义上是相同的。您必须使用括号对析取进行分组,使其优先于连接:

SELECT * 
FROM  `nam_order_items` 
WHERE item_status =  ('Glass Bending - Andy'
OR item_status =  'Glass Bending - Bren'
OR item_status =  'Glass Bending - Gary'
OR item_status =  'Glass Bending - James'
OR item_status =  'Glass Bending - Oscar' )
AND `backing_cut` = 0

答案 2 :(得分:0)

为了更容易理解为什么要按照您熟悉的术语考虑运营商。

ORAND运算符优先级类似于+*

因此,如果您执行1 + 1 + 1 * 21 + 1 + 1 * (2)1 + 1 + 1 (* 2),则结果始终为4*始终是第一个执行的运算符。 如果您希望结果为6,那么您需要(1 + 1 + 1) * 2