0

I have the following prepared statement query:

 SELECT id FROM CustomerSettings WHERE id  >= ? OR displayNameType  = ? AND id  < ? LIMIT 5 OFFSET 1

The "id <" part of the query is ignored. Curiously, if I move the statement BEFORE the OR statement then it is correctly evaluated by the MySQL database engine. Is this expected behaviour seems rather odd. I have also tried putting the OR clause in brackets this unfortunately didn't work either; the AND clause immediately following the OR is still ignored.

Giles Thompson
  • 1,097
  • 1
  • 9
  • 24
  • It's hard to see exactly what result you are looking for. `AND` binds more tightly than `OR`, so MySQL parses yours as `WHERE id >= ? OR (displayNameType = ? AND id < ?)` If that isn't what you want, then the alternative may not make sense `(id >= ? OR displayNameType = ?) AND id < ?` because `id` may not be able to meet both conditions. – Michael Berkowski May 11 '15 at 00:49

1 Answers1

5

This is due to operator precedence. AND binds before OR so your query is WHERE (id >= ?) OR (displayNameType = ? AND id < ?). The order of things matters. I'd suggest using parentheses to make the order of AND and OR explicit.

Oliver Dain
  • 9,617
  • 3
  • 35
  • 48