SELECT 
    AVG(IF(second_day IS NULL, 0, 1)) AS avg_ret
FROM (
    SELECT DISTINCT
        t1.device_id AS device_id,
        t1.date AS first_day,
        t2.date AS second_day
    FROM question_practice_detail t1
    LEFT JOIN question_practice_detail t2
        ON DATEDIFF(t2.date, t1.date) = 1 and t1.device_id = t2.device_id
) t

使用自连接的方法,以device_id以及date之差为1作为条件,做左连接。得到的结果中t2.date这一列为NULL的即为第二天没有再来刷题的用户。在主查询中使用IF语句配合这一列的查询结果,使用聚合函数求平均值。