0

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`` orautogoal```

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:

  1. user_group=admin, user_subscription=```all``

  2. user_group=allow, user_subscription=```autogoal``

I always get only the second entry.

2
  • add parenthesis in your query: (c1 OR c2) AND (c3 OR c4) Commented Jul 18, 2019 at 16:09
  • Tables don't have "cells", they have columns in rows. Thinking of a table as though it's a spreadsheet will cause nothing but pain. :) Commented Jul 18, 2019 at 16:22

2 Answers 2

1

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") 
Sign up to request clarification or add additional context in comments.

2 Comments

Ty, this work in DB Browser for SQLite but not work in python. Python give me anyway first one result. U dont know why this is?
It should work in Python also, if you write it correctly and you query the same table. Check again.
0

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.