0

What happens if both values in an or statement are non-null?

Does mysql uses the left side every time or is there a possibility that mysql uses the right side?

SELECT * 
FROM posts p 
WHERE parent_id = ? OR id = ?
ORDER BY date 
DESC LIMIT 1
user2429266
  • 390
  • 1
  • 3
  • 19
  • Of course will mysql use the right side. Why not? Do you want the check of the id only when there's no matching parent_id at all? – VMai May 09 '14 at 20:25
  • 1
    possible duplicate of [Short-circuit logic evaluation operators](http://stackoverflow.com/questions/16969780/short-circuit-logic-evaluation-operators) – Marcus Adams May 09 '14 at 20:42

1 Answers1

0
  1. If the values are null then the comparison with = will fail. Use IS instead.
  2. You can't really tell which condition will be executed first and how the DB handles the filtering. This is totally up to the optimizer at runtime.
juergen d
  • 201,996
  • 37
  • 293
  • 362