mysql - 计算两个日期之间的用户数

标签 mysql

我有一个只有两个字段的表 - 日期字段和 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/

相关文章:

MySQL 插入不正确的 CRC32 哈希值

mysql - 在 innoDB 中优化 MySQL LIKE '%string%' 查询

mysql - 如何进行 Access 内连接

mysql - 遇到问题 : Illegal mix of collations in MySQL

mysql - 对在mysql中创建表感到困惑

php - 如何更改标题以及它们在 WordPress 中的显示方式?

mysql - 如何从 MySQL 中的字符串中删除 url?

php - SQL/PHP 查询优化

mysql - 检查表列中的变量数组时使用哪个 mySQL 运算符

mysql - 如何知道mysql中的列何时创建?