首页 文章

MySQL:找到在网站上注册然后当天购买的用户

提问于
浏览
0

我正在尝试捕获在我们网站上注册然后在同一天购买的用户数量 . 在我的查询上有点迷失 . 它返回结果,但经过仔细检查(这些是乐福鞋),结果不正确 .

我有2个表: customerpurchase . 两个表都有 customer_iddate (对于customer表,日期为"date_created",购买表为"date_modified") .

基本上我想得到每天在 customer_id 表中创建的user_ids的聚合计数,然后购买当天出现在 purchase 表中 . *注意:有's also a condition where I don' t想要包含来自customer_id的购买1. customer_id 1是一个测试帐户,有时用于在网站上进行测试购买 .

这是我试过的:

select 
    date_format(c.`date_created`, '%Y-%m-%d') as day_date
    ,x.same_day_purchase

from `customer` c

left join (
    select 
        p.`customer_id`
        ,p.`date_modified`
        ,sum(if(p.`date_modified` >= cp.`date_created`,1,0)) as same_day_purchase
    from purchase p
    inner join `customer` cp on p.`customer_id` = cp.`customer_id` and p.`customer_id`<>1

group by cp.`date_created`
            ) x on c.`customer_id` = x.`customer_id` and x.`customer_id`<>1


where (c.`date_created` >= '2011/09/01' and c.`date_created` < '2014/05/14')
group by day_date

(所以我要找的结果是:

2011/09/01 - 3

2011/09/02 - 16

等等

因此,在2011年9月1日,3位用户来到我们的网站并注册,然后在当天购买了一些东西 . 第二天我们有16人新注册的客户购买产品,等等....)

谢谢!

2 回答

  • 1

    这是您的出发点:

    select date(date_created)
           ,count(distinct c.customer_id)
    from customer c
    join purchase p using (customer_id)
    where DATEDIFF(p.date_modified,c.date_created) = 0
           and c.date_created between '2011/09/01' and now()
           and c.customer_id <> 1
    group by date(c.date_created)
    
  • 2

    你可以试试这个

    SELECT DATE(c.date_created) AS `date`
           ,COUNT(DISTINCT c.customer_id) AS registered_customers_who_bought
    FROM customer AS c
    JOIN purchase AS p
      ON c.customer_id = p.customer_id
        AND DATE(c.date_created) = DATE(p.date_modified)
    WHERE (c.`date_created` >= '2011/09/01' and c.`date_created` < '2014/05/14')
    GROUP BY DATE(c.date_created)
    

相关问题