Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 谁知道每天有一笔交易的用户数量?_Mysql_Sql - Fatal编程技术网

Mysql 谁知道每天有一笔交易的用户数量?

Mysql 谁知道每天有一笔交易的用户数量?,mysql,sql,Mysql,Sql,我的问题是: select count(1) from (select count(1) num, user_id from pos_transactions pt where date(created_at) <= '2020-6-21' group by user_id having num = 1) x 它给出了到2020年6月21日为止拥有1笔交易的用户数量。现在我想把它也按日期分组。我的意思是,我想得到一个日期列表,比如2020-6-21

我的问题是:

select count(1) from 
    (select count(1) num, user_id from pos_transactions pt 
    where date(created_at) <= '2020-6-21'
    group by user_id  
    having num = 1) x
它给出了到2020年6月21日为止拥有1笔交易的用户数量。现在我想把它也按日期分组。我的意思是,我想得到一个日期列表,比如2020-6-21、2020-6-22等等。。加上当天有1笔交易的用户数

知道我该怎么做吗


编辑:上面查询的结果是正确的,问题是,现在是手工的。我的意思是,我必须手动增加2020-6-21。我想自动完成。换句话说,我想要一个从2020年6月21日到现在的所有日期的列表,其中包含到该日期为止有1笔交易的用户数量。

如果您想要每天有一笔交易的用户数量,那么您还需要按日期进行聚合:

select dte, count(*)
from (select date(created_at) as dte, user_id
      from pos_transactions pt 
      where date(created_at) <= '2020-6-21'
      group by dte, user_id  
      having count(*) = 1
     ) du
group by dte;

请遵循此指南改进您的问题:为什么不将datecreated_at添加到分组列表中?