MySQL 文档有一个好页面 http://dev.mysql.com/doc/refman/5.0/en/operator-precedence.html以及有关哪些运算符优先的信息。
从该页面开始,
12.3.1.运算符优先级
运算符优先级如下表所示,从最高优先级到最低优先级。运营商认为
一起显示在一行上,具有相同的优先级。
INTERVAL
BINARY, COLLATE
!
- (unary minus), ~ (unary bit inversion)
^
*, /, DIV, %, MOD
-, +
<<, >>
&
|
= (comparison), <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN
BETWEEN, CASE, WHEN, THEN, ELSE
NOT
&&, AND
XOR
||, OR
= (assignment), :=
所以你原来的查询
Select
*
from tablename
where
display = 1
or display = 2
and content like "%hello world%"
or tags like "%hello world%"
or title = "%hello world%"
将被解释为
Select
*
from tablename
where
(display = 1)
or (
(display = 2)
and (content like "%hello world%")
)
or (tags like "%hello world%")
or (title = "%hello world%")
如有疑问,请使用括号来明确您的意图。虽然 MySQL 页面上的信息很有帮助,但如果重新访问该查询,这些信息可能不会立即显而易见。
您可能会考虑类似以下内容。请注意,我已经更改了title = "%hello world%"
to title like "%hello world%"
,因为这更符合您所描述的目标。
Select
*
from tablename
where
(
(display = 1)
or (display = 2)
) and (
(content like "%hello world%")
or (tags like "%hello world%")
or (title like "%hello world%")
)