Sql 问题清单比较
我的个人资料如下所示:Sql 问题清单比较,sql,Sql,我的个人资料如下所示: profile_id | answer_id ---------------------- 1 1 1 4 1 10 user_id | answer_id ------------------- 1 1 1 9 2 1 2 4 2 10 3 14 3
profile_id | answer_id
----------------------
1 1
1 4
1 10
user_id | answer_id
-------------------
1 1
1 9
2 1
2 4
2 10
3 14
3 29
我有一个表格,其中包含了民意调查受访者的回答列表,结构如下:
profile_id | answer_id
----------------------
1 1
1 4
1 10
user_id | answer_id
-------------------
1 1
1 9
2 1
2 4
2 10
3 14
3 29
如何选择在配置文件中给出所有答案的用户列表?在这种情况下,只有用户2
SELECT *
FROM table1
INNER JOIN table2
ON table1.answer_id = table2.answer_id
WHERE table2.user_id = 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
看
或者用另一种方式来写:
select user_id
from response r
where answer_id in (1, 4, 10)
group by user_id
having count(distinct answer_id) = 3
请参见您可以使用以下选项:
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
看
或者用另一种方式来写:
select user_id
from response r
where answer_id in (1, 4, 10)
group by user_id
having count(distinct answer_id) = 3
请参见这是一个包含聚合的联接查询示例:
select a.user_id
from profile p full outer join
answers a
on p.answer_id = p.answer_id and
p.profile_id = 1
group by a.user_id
having count(p.profileid) = count(*) and
count(a.user_id) = count(*)
完全外部联接将所有配置文件与所有答案匹配。如果两个集合完全匹配,则另一个集合的ID中没有“null”。
having
子句检查jsut是否满足此条件。这是一个包含聚合的连接查询示例:
select a.user_id
from profile p full outer join
answers a
on p.answer_id = p.answer_id and
p.profile_id = 1
group by a.user_id
having count(p.profileid) = count(*) and
count(a.user_id) = count(*)
SELECT user_id
FROM user_answer
WHERE user_id in (SELECT user_id FROM profile WHERE answer_id = 1) AND
user_id in (SELECT user_id FROM profile WHERE answer_id = 4) AND
user_id in (SELECT user_id FROM profile WHERE answer_id = 10)
完全外部联接将所有配置文件与所有答案匹配。如果两个集合完全匹配,则另一个集合的ID中没有“null”。
having
子句检查jsut此条件。这不会返回回答了所有问题的用户。这不会返回回答了所有问题的用户。你在使用什么rdbms?你在使用什么rdbms?这太可怕了。。。为什么不使用连接?我应该澄清一下。您正在毫无理由地对用户ID进行3次单独的索引扫描。你可以使用1.KISS=保持简单愚蠢:)嗯,这是一个有效的参数;-)。但我不确定它是否能在数据库中工作,在数据库中效率通常是一切…如果你只需要杀死一只黄蜂,为什么要制造火箭:)这太可怕了。。。为什么不使用连接?我应该澄清一下。您正在毫无理由地对用户ID进行3次单独的索引扫描。你可以使用1.KISS=保持简单愚蠢:)嗯,这是一个有效的参数;-)。但我不确定它是否能在数据库中工作,在数据库中,效率通常是一切……如果你只需要杀死一只黄蜂,为什么要制造火箭:)
SELECT user_id
FROM user_answer
WHERE user_id in (SELECT user_id FROM profile WHERE answer_id = 1) AND
user_id in (SELECT user_id FROM profile WHERE answer_id = 4) AND
user_id in (SELECT user_id FROM profile WHERE answer_id = 10)