mysql select when matching multiple rows

Posted by user1735943 on Stack Overflow See other posts from Stack Overflow or by user1735943
Published on 2012-12-05T04:39:16Z Indexed on 2012/12/05 5:03 UTC
Read the original article Hit count: 85

Filed under:
|

I have a project where I need to select only the users that answered to some questions in a certain way (based on a filter).

The filter table (filter) looks like this

   question |   answer  

   Q1       |    A  
   Q2       |    B 

The user table (answers) looks like this

user | question | answer  

  1  |    Q1    |    A  
  1  |    Q2    |    D  
  2  |    Q1    |    A  
  2  |    Q2    |    B 

How can I select from the user table only the user(s) that match the filter?
I tried

"SELECT user FROM answers WHERE (question = Q1 AND answer = A) 
             AND (question = Q2 AND answer = B)" 

and it doesn't work -- I get an empty result. Thank you.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about query