检查满足特定条件的最大连续天数

检查满足特定条件的最大连续天数,第1张

检查满足特定条件的最大连续天数

只要在users_id和brinkles_id上有一个复合索引,此解决方案的效果似乎就很好-

SELECt *FROM (    SELECt t.*, IF(@prev + INTERVAL 1 DAY = t.d, @c := @c + 1, @c := 1) AS streak, @prev := t.d    FROM (        SELECt DATE(timestamp) AS d, COUNT(*) AS n        FROM beverages_log        WHERe users_id = 1        AND beverages_id = 1        GROUP BY DATE(timestamp)        HAVINg COUNT(*) >= 5    ) AS t    INNER JOIN (SELECT @prev := NULL, @c := 1) AS vars) AS tORDER BY streak DESC LIMIT 1;


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5431746.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存