Mysql或/和优先级?
我想知道or/and是如何工作的 例如,如果我想获取display=1的所有行 我只需执行Mysql或/和优先级?,mysql,operator-precedence,Mysql,Operator Precedence,我想知道or/and是如何工作的 例如,如果我想获取display=1的所有行 我只需执行即可,其中tablename.display=1 如果我想要显示=1或2的所有行 我只需执行即可,其中tablename.display=1或tablename.display=2 但是,如果我想获取display=1或2的所有行,以及内容、标记或标题包含hello world 这样的逻辑会是怎样的呢 Select * from tablename where display = 1 or display
即可,其中tablename.display=1
如果我想要显示=1或2的所有行
我只需执行即可,其中tablename.display=1或tablename.display=2
但是,如果我想获取display=1或2的所有行,以及内容、标记或标题包含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%"
我猜是吧。但是我可以从几个方面来理解
其内容是否为:
(display = 1 or display = 2) and (content like "%hello world%" or tags like "%hello world%" or title = "%hello world%")
或作为
等等。在所有SQL Server中,
和
优先于或
,因此请记住在或
周围放上括号:
select * from tablename
where (display = 1 or display = 2)
and (content like "%hello world%"
or tags like "%hello world%"
or title = "%hello world%")
顺便说一句,(display=1或display=2)相当于(1,2)中的
显示您需要为多个或条件使用括号。对于display=1或display=2
,您可以使用display IN(1,2)
。试试这个:
SELECT * FROM tableName
WHERE display IN (1,2)
AND (content LIKE "%hello world%"
OR tags LIKE "%hello world%"
OR title LIKE "%hello world%")
有关更多信息,请参阅,MySQL文档中有一个包含哪些运算符优先的信息
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), :=
从那一页
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%")
当有疑问时,用括号说明你的意图。虽然MySQL页面上的信息很有帮助,但如果再次访问查询,可能不会立即显示出来
你可以考虑如下的事情。请注意,我已将标题=“%hello world%”
更改为标题,如“%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%")
)
运行此查询:
select 1 or 1 and 0
如果结果为1
,则表示优先级为:
select 1 or (1 and 0)
select (1 or 1) and 0
如果结果为0
,则优先级为:
select 1 or (1 and 0)
select (1 or 1) and 0
扰流板:它出来了1
也就是说,和
s是在或
s之前进行评估的,或者正如我喜欢说的,AND比较难学。这些东西是在你早期的C/C++课程中教授的。那么我想我应该报名参加整个C/C++课程,只是为了学习运算符优先级:/请参阅我对我问题的补充。这与没有括号有关,结果如何?