【分类】:聚合函数、select if

分析思路

难点:

1.如何解决对于在8月份没有练习过的用户,答题数结果返回0?

对于在8月份没有练习过的用户,答题数结果返回0

  • [使用]:month(attr.date) = 8 or month(attr.date) is null

求解代码

select
    main.device_id,
    main.university,
    count(question_id) as question_cnt,
    count(if(attr.result = 'right', 1, null)) as right_question_cnt
from user_profile main
left join question_practice_detail attr on main.device_id = attr.device_id
where main.university = '复旦大学'
and(month(attr.date) = 8 or month(attr.date) is null)
group by main.device_id