我正在努力进行正确的 mysql 查询。这是表格:
**survey_result**
question_id
header_id
answer
其中答案是 (0,1,2,3,4,5) 范围内的 int。我想进行查询,我将计算特定 id 出现了多少次,特定答案发生了。所以对于 question_id = 1 我想看看:
question_id answer occurence_number
1 0 12
1 1 20
1 2 34
1 3 5
1 4 9
1 5 15
而且很可能我想选择一个出现次数最多或最少的答案。但这是第二部分,我正在为第一部分苦苦挣扎。
在第一条语句的巨大帮助下,我设法完成了它,这就是工作版本的样子:
select question_id, max(occurence_number) FROM
(select question_id, answer, count(*) as occurence_number
from survey_result
group by question_id, answer
order by question_id asc, occurence_number desc) as results
GROUP BY question_id