Reputation: 49
I am working on a project for a data bootcamp that I am in, and I am working with three tables. Within these tables, I am trying to find a little more information about the users in this data set. Some of the questions I would like to answer are...
Attached is an overview of my table (ignore the crap code) This is the query I am having problems with:
select user_id, q2, q3, q4
from survey
where q2 = '0' or '1' and q3 = '0' or '1' or '2' or '3' and q4 = '0' or '1' or '2' or '3' or '4' or '5'
I want to pull up the named values for q2, q3, q4. How do I write this correctly so that it pulls the data in one set? I would like to pull it in one set so that I can export and import into excel and pivot the data.
Upvotes: 0
Views: 57
Reputation: 37299
Two problems:
and
and or
make sure you wrap properly with ()
..or
you must specify again the comparison. q3 = '0' or '1'
won't work -> change to q3 = '0' or q3 = '1'
So:
select user_id, q2, q3, q4
from survey
where (q2 = '0' or q2 = '1')
and (q3 = '0' or q3 = '1' or q3 = '2' or q3 ='3')
and (q4 = '0' or q4 = '1' or q4 = '2' or q4 = '3' or q4 = '4' or q4 = '5')
Now, a nicer way to do this is to use in
select user_id, q2, q3, q4
from survey
where q2 in ('0','1')
and q3 in ('0','1','2','3')
and q4 in ('0','1','2','3','4','5')
Upvotes: 4
Reputation: 350252
The thing that is wrong with your query is that you cannot write q2 = '1' or '2'
. It is invalid syntax. Use in
instead:
where q2 in ('0','1') and q3 in ('0','1','2','3') and q4 in ('0','1','2','3','4','5')
Upvotes: 3