-1

Possible Duplicate:
Mysql or/and precedence?

If I have a query that looks like this:

SELECT * 
  FROM `users`
 WHERE 'gen'='0' OR `gen`='1' AND `id` IN ('405','406','407',...),

will it return the results from users where (gen='0' or gen='1') and (id IN (...), or some other result? if so, can i put parentheses around the clauses to separate?

thanks.

Community
  • 1
  • 1
user988129
  • 73
  • 2
  • 3
  • 10
  • sorry - there should be an AND before the IN. assuming the AND, would it work? – user988129 Oct 05 '12 at 16:14
  • Your query above will not evaluate your `AND ID IN` condition if `gen=0`. Yes you can use parentheses and write your where clause like - `WHERE ('gen'='0' OR 'gen'='1') AND 'id' IN ('405','406','407',...)` – rs. Oct 05 '12 at 16:18
  • FWIW, When mixing OR and AND in the same statement I find it a lot easier to read if you explicitly put parentheses where you indent them to be, even if they are not truly necessary – lc. Oct 05 '12 at 16:23

1 Answers1

0

Try,

SELECT * 
FROM `users`
WHERE `gen` IN (0, 1) AND 
      `id` IN ('405','406','407',...)

is the same as

SELECT * 
  FROM `users`
 WHERE (`gen`='0' OR `gen`='1') AND 
        `id` IN ('405','406','407',...)
John Woo
  • 258,903
  • 69
  • 498
  • 492