开发者

Where 'foo' OR 'bar' AND 'lol' OR 'rofl' MySQL

In what order would this be evaluated. My intension is that if it finds either foo or bar, i开发者_运维问答t would also search for lol and rofl.

Is this totally in the woods? And if so, how would one evaluate an expression like that.


The AND operator has higher precedence than OR in MySql, so your current expression evaluates as:

WHERE 'foo' OR ('bar' AND 'lol') OR 'rofl'

Add parentheses to the expression if you want to force the evaluation order:

WHERE ('foo' OR 'bar') AND ('lol' OR 'rofl')


AND will be processed first, after that OR will be processed. So, it will be:

'foo' OR ('bar' AND 'lol') OR 'rofl'

After that, it is left to right order.


take a look at the documentation - AND has a higher precedence, so it would be like this:

WHERE 'foo' OR ( 'bar' AND 'lol' ) OR 'rofl'


SELECT
  Id, Name
FROM
  TestTable
WHERE
    (Name = 'foo') OR (Name = 'bar') AND (Name = 'lol') OR (Name = 'rofl')

Will give you the following result in MS SQL Server:

1 foo
4 rofl

So it seems it will combine bar AND lol and evals foo and rofl seperately like this:

SELECT
      Id, Name
    FROM
      TestTable
    WHERE
        (Name = 'foo') OR ((Name = 'bar') AND (Name = 'lol')) OR (Name = 'rofl')

What you probably want to do is (technically):

SELECT
      Id, Name
    FROM
      TestTable
    WHERE
        ((Name = 'foo') OR (Name = 'bar')) AND ((Name = 'lol') OR (Name = 'rofl'))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜