看了几个题发现对第二个条件至少有一次用了一半时间就完成高难度试卷且分数大于80的人(activity2)发了福利券的处理都是放在where里筛选,其实当然可行,毕竟这道题之后直接group by uid那么不影响uid出现次数。
我就写一下至少一次的满足条件sql写法吧:
select uid,
'activity1' as activity
from exam_record
where year(start_time)=2021
group by uid
having count(if(score>=85,1,null))=count(start_time)
union ALL
select uid,
'activity2' as activity
from exam_record
left join examination_info using(exam_id)
where difficulty='hard' and score>80 and year(start_time)=2021
group by uid
having count(if(timestampdiff(second,start_time,submit_time)/60<=duration/2,1,null))>=1
order by uid asc