3

我有一个项目,我需要只选择以某种方式(基于过滤器)回答某些问题的用户。

过滤表(filter)长这样

   question |   answer  

   Q1       |    A  
   Q2       |    B 

用户表(答案)如下所示

user | question | answer  

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

如何从用户表中仅选择与过滤器匹配的用户?
我试过

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

它不起作用——我得到一个空的结果。谢谢你。

4

3 回答 3

3

在您的查询中,您要求获取不存在的数据。您正在尝试获取同时具有这两种组合的用户。

试试这个

SELECT user FROM answers WHERE (question = 'Q1' AND answer = 'A') 
         OR (question = 'Q2' AND answer = 'B') 
于 2012-12-05T05:31:08.660 回答
2

试试这个:

Select 
DISTINCT(userId)
from user_table ut
inner join filtertable ft on (ut.question=ft.question and ut.answer=ft.answer)
于 2012-12-05T04:41:43.203 回答
2

试试这个

select answers.user
from filter, answers
where filter.question=answers.question and filter.answer=answers.answer
group by answers.user having count(answers.question)=2

或者

select user
from answers
where user not in 
(select distinct a.user from answers a, filter f 
where a.question=f.question and a.answer!=f.answer)
于 2012-12-05T05:00:31.400 回答