I know I must be missing something simple here...but I'm having problems with writing an SQL sub query. Given the following data
user_id question_id answer_text
89 1 value1
89 2 value2
80 2 value2
99 2 value2
96 1 value1
96 2 value2
111 1 value1
111 2 value2
I need to get the user_id's that have BOTH question_id 1 = 'value1' AND have question_id 2 = 'value2'
The results above are generated using this query:
SELECT `User_answer`.`user_id`, `User_answer`.`question_id`, `User_answer`.`answer_text` FROM `user_answers` AS `User_answer` WHERE `User_answer`.`question_id` IN (1, 2) AND `User_answer`.`answer_text` IN ('value1', 'value2')
but of course this returns users that have answered question 1 with "value1" but question 2 with a completely different answer than "value2" (and vice versa).
I need to be able to add in more conditions but I think I can add that if I can get at least these two working.