2010-08-11 69 views
0

是这样的:MySQL的:和/或质疑

update batchinfo set instrument='Instrument 17' where 
datapath like '%10497%' or 
datapath like '%10506%' or 
datapath like '%10516%' or 
datapath like '%11081%' 
and instrument='Instrument 1' 

与此相同:

update batchinfo set instrument='Instrument 17' where 
datapath like '%10497%' and instrument='Instrument 1' or 
datapath like '%10506%' and instrument='Instrument 1' or 
datapath like '%10516%' and instrument='Instrument 1' or 
datapath like '%11081%' and instrument='Instrument 1' 

回答

2

否 - 也是唯一有资格datapath like '%11081%'

不要冒险 - 使用括号。他们更便宜。

2

没有,但是这两者是等价的:

update batchinfo 
set instrument='Instrument 17' 
where 
    (datapath like '%10497%' or 
    datapath like '%10506%' or 
    datapath like '%10516%' or 
    datapath like '%11081%') 
    and instrument='Instrument 1' 

update batchinfo 
set instrument='Instrument 17' 
where 
    (datapath like '%10497%' and instrument='Instrument 1') or 
    (datapath like '%10506%' and instrument='Instrument 1') or 
    (datapath like '%10516%' and instrument='Instrument 1') or 
    (datapath like '%11081%' and instrument='Instrument 1') 
2

号见mysql的操作precedence chart。一般来说,如果混合使用ANDOR括号中的内容是个不错的主意。

+0

好吧然后我搞砸了谢谢你有一个愉快的一天 – 2010-08-11 16:50:43

+0

AND'将始终优先于'OR',而不仅仅是在MySQL中。 – FrustratedWithFormsDesigner 2010-08-11 16:54:11

+0

@Frustrated不一定。在那里有一些奇怪的运算符优先规则。短暂聊天。出于这个原因,我是一个狂热的狂热分子。 – deinst 2010-08-11 17:03:34