本题类型和之前类似,相比if函数我比较常用case when daydiff和timestampdiff 前者是日期差,后者是时间差更为灵活

 TIMESTAMPDIFF(day/hour/second....., '小时间', '大时间')

百分比的表示则先把avg*100 再用concat和%链接即可 因为是计算出答案后在对答案进行筛选 所以用having

select tag,concat(round(avg(case when TIMESTAMPDIFF(second, start_time, end_time)<=duration
                   then TIMESTAMPDIFF(second, start_time, end_time)/duration
               else 1 end)*100,2),'%') as AVG_play_progress
from tb_user_video_log vid
    join tb_video_info info
    on vid.video_id=info.video_id

group by tag
having SUBSTRING_INDEX(AVG_play_progress,"%",1)>60
order by AVG_play_progress desc