sql - 显示每个答案计数的用户ID计数

时间:2014-11-11 01:57:38

标签: sql mysqli

我有一个包含唯一用户回答的答案数据的表格。所以我有这个问题。

select ta.user_id, count(ta.answer_id) count_answer_id 
from survey_answers ta 
group by ta.user_id 
having count(ta.answer_id) < 124
order by count_answer_id desc;



userid  count of answers
3702    123
2866    120
5483    120
565     120
1292    120
621     119
2250    119
2719    119
4192    119
5539    119
354     119
1441    119
2501    115
1636    115
866     109
53      108
3091    107
329     106
285     105
997     104
1352    103
5281    103
430     102
2125    102

但我想得到这样的结果。

user count    answer count
1               123
4               120
7               119
2               115
.
.
.
.

1 个答案:

答案 0 :(得分:4)

select count(*) as user_count, count_answer_id 
from
(
  select ta.user_id, count(ta.answer_id) count_answer_id 
  from survey_answers ta 
  group by ta.user_id 
  having count(ta.answer_id) < 124 
) tmp
group by count_answer_id 
order by count_answer_id desc