2

If you preform an if in PHP it will stop the conditions after the first false in the if-statement.

Has MySQL the same behavior in its where condition?

Suppose I have a query:

SELECT id FROM table_name WHERE row = value AND EXISTS(...)

Will the EXISTS statement be executed if row = value is false?

JasperV
  • 656
  • 1
  • 9
  • 19
  • [this input](http://stackoverflow.com/questions/5542927/sql-server-conditional-flow) might give you answers on your question. – KarelG Mar 12 '14 at 09:30
  • You can create a [Query Execution Plan](https://dev.mysql.com/doc/refman/5.5/en/execution-plan-information.html) to get the specific answer for your particular query – Andreas Fester Mar 12 '14 at 09:34
  • In SQL, in general, you're telling the system "what you want", not "how to do it" - and one of the things you don't tend to get control over is what order it performs individual tasks in. – Damien_The_Unbeliever Mar 12 '14 at 10:35

1 Answers1

1

There is no guarantee that the where conditions will be examined in the same order as they appear in the query. The execution plan will give you the answer,try

EXPLAIN SELECT id FROM table_name WHERE row = value AND EXISTS(...)

The plan may also vary depending on the table's statistics.

Paul J
  • 476
  • 2
  • 8