我有user_answers
表:
id | quiz_id | user_id | question_id | choosen_answer_id |
1 | 1 | 2 | 1 | 5 |
2 | 1 | 2 | 2 | 6 |
3 | 1 | 2 | 3 | 7 |
4 | 1 | 3 | 1 | 5 |
5 | 1 | 3 | 2 | 8 |
6 | 1 | 3 | 3 | 9 |
我想cont
与{{1}的choosen_answer_id
用户相比,{{1}的用户有多少正确答案(相同question_id
的相同user_id = 3
) }}
在此示例中,我应该收到user_id = 2
,因为具有quiz_id
的用户仅对问题为id_id = 1的问题回答正确(对于user_id = 2,question_id = 1 selectn_answer_id = 5,对于user_id = 3,question_id = 1 choicen_answer_id = 5`)。
谢谢
答案 0 :(得分:1)
我了解您愿意考虑两个用户给出相同答案的问题数量。您可以按如下方式自动加入表格:
SELECT COUNT(*) AS number_of_identical_answers
FROM user_answers ua2
INNER JOIN user_answers ua3
ON ua3.user_id = 3
AND ua3.quiz_id = ua2.quiz_id
AND ua3.question_id = ua2.question_id
AND ua3.choosen_answer_id = ua2.choosen_answer_id
WHERE ua2.user_id = 2
如果要显示合并的记录而不是对它们进行计数,则可以将COUNT(*)
更改为表ua2
(用户2的答案)和ua3
(答案的表)的列列表用户3)。
答案 1 :(得分:0)
select user_id, count(*)
from table1 t1
join (select *from table1 where user_id = 2) t2
on t2.question_id = t1.question_id
where t2.choosen_answer_id = t1.choosen_answer_id
group by t1.user_id