我的个人资料看起来像这样:
profile_id | answer_id
----------------------
1 1
1 4
1 10
Run Code Online (Sandbox Code Playgroud)
我有一个表格,其中包含一些受访者的回复列表,其结构如下:
user_id | answer_id
-------------------
1 1
1 9
2 1
2 4
2 10
3 14
3 29
Run Code Online (Sandbox Code Playgroud)
如何选择在配置文件中提供所有答案的用户列表?在这种情况下只有用户2.
您可以使用以下内容:
select user_id
from response r
where answer_id in (select distinct answer_id -- get the list of distinct answer_id
from profile
where profile_id = 1) -- add filter if needed
group by user_id -- group by each user
having count(distinct answer_id) = (select count(distinct answer_id) -- verify the user has the distinct count
from profile
where profile_id = 1) -- add filter if needed
Run Code Online (Sandbox Code Playgroud)
或者另一种写这个的方法是:
select user_id
from response r
where answer_id in (1, 4, 10)
group by user_id
having count(distinct answer_id) = 3
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
49 次 |
| 最近记录: |