axew3
axew3

Reputation: 311

MySQL WHERE OR: if first condition is true, the second is executed?

There is some another question about, but i didn't have still understand this despite all answers. The question is, in a query like this:

select * from table WHERE userid = userid OR username = username

if is found true the first condition, userid = userid , mysql follow to test also the second condition OR username = username ??? or NOT? I think yes, but would like to know as i can't find out an answer on any documentation. Thank you.

Upvotes: 7

Views: 4901

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269753

There is no "first condition" and "second condition". MySQL can choose to execute the conditions in whichever order it wants. A SQL query represents the result set, not the specific processing steps.

That said, if multiple conditions are connected by or or and, then MySQL will "short-circuit" the evaluation and stop when it knows the answer.

The documentation is a bit weak on this point (it doesn't explain it exactly). So, it is possible that MySQL (unlike virtually every other SQL database and compiler) does not short-circuit when possible. Here is a blog where the author actually did a test on this; so there is at least one instance where the compiler appears to short-circuit the evaluation.

Upvotes: 8

Related Questions