Reputation: 956
I have a table in which cells can be as follows:
user_group
can be admin
, allow
, disallow
user_subscription
can be all
, penalty
, autogoal
I need to get the records that correspond to the following expression:
All admin
and allow
where user_subscription
== all`` or
autogoal```
I tried to do it like this:
('SELECT * FROM USERS WHERE (user_group="allow" OR user_group="admin" AND user_subscription="autogoal" OR user_subscription="all")')
But it does not work. I have 2 entries in the database:
user_group
=admin
, user_subscription
=```all``
user_group
=allow
, user_subscription
=```autogoal``
I always get only the second entry.
Upvotes: 0
Views: 38
Reputation: 164069
You must use parentheses correctly, because the AND
operator has higher precedence than the OR
operator.
So your WHERE condition is equivalent to:
user_group="allow"
OR
(user_group="admin" AND user_subscription="autogoal")
OR
user_subscription="all"
Write the statement like this:
SELECT * FROM USERS
WHERE
(user_group="allow" OR user_group="admin")
AND
(user_subscription="autogoal" OR user_subscription="all")
You can find more here.
Or with the IN
operator:
SELECT * FROM USERS
WHERE
user_group IN ("allow", "admin")
AND
user_subscription IN ("autogoal", "all")
Upvotes: 1
Reputation: 11064
You are missing some parenthesis in your WHERE clause
Try this :
SELECT *
FROM USERS
WHERE (user_group="allow" OR user_group="admin")
AND (user_subscription="autogoal" OR user_subscription="all")
Upvotes: 0