我有一个只有两个字段的表 - 日期字段和 customer_id。我希望计算从每个日期字段到当前日期的客户 ID 数量。我下面的查询超时 - 似乎效率很低。有更好的方法吗?
select
t.base_date,
( select
count(distinct customer_id)
from user_base as ub
where ub.base_date >= t.base_date
and ub.base_date <= current_date
) as cts
from user_base as t
最佳答案
尝试一下,如果这给您带来了相同的结果(未经测试),但似乎您提取数据的方式不是正确的做法:
select base_date, count(distinct customer_id) as cts
from user_base
where base_date between base_date AND current_date
关于mysql - 计算两个日期之间的用户数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40554679/