I got a table with username, usergroup and usertype.
A 0 0
B 0 1
C 1 2
D 2 2
Now i want something like
get all the users where the group is 0 or 1 and type is 2
I use:
SELECT * FROM table WHERE usergroup= 0 OR 1 AND usertype=2 GROUP BY usergroup
I expect it will return the username C, but strangely it returns A,C,D
What am I doing wrong?
I’m going to ignore that
GROUP BY.First, you have precedence issues; your SQL query is equivalent to:
Use parentheses to be clear:
There’s still a problem, in that boolean operators simply don’t work like this:
0 OR 1is an expression that evaluates to1.You have to repeat the thing you’re comparing against:
However, this can be shortened thus: