Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.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 在SQL中查找15天内的所有活动用户_Mysql_Sql_Nested Queries - Fatal编程技术网

Mysql 在SQL中查找15天内的所有活动用户

Mysql 在SQL中查找15天内的所有活动用户,mysql,sql,nested-queries,Mysql,Sql,Nested Queries,我有两个表:users,user\u source\u history 在“表用户”中,保存所有已注册的用户。并且,在表user\u source\u history中,存储了它们的活动。这些活动可能包括登录系统、订购物品。因此,一个用户可以在用户源历史中存在多次 现在的问题是,我需要找到15天内的所有活跃用户 例如,一个用户在2014-12-03购买了一些东西。现在,该用户将在2014年12月18日之前显示为活动用户 我需要找到日期范围内的所有活动用户 例如,在这个日期范围内- 2014-12

我有两个表:users,user\u source\u history

在“表用户”中,保存所有已注册的用户。并且,在表user\u source\u history中,存储了它们的活动。这些活动可能包括登录系统、订购物品。因此,一个用户可以在用户源历史中存在多次

现在的问题是,我需要找到15天内的所有活跃用户

例如,一个用户在2014-12-03购买了一些东西。现在,该用户将在2014年12月18日之前显示为活动用户

我需要找到日期范围内的所有活动用户

例如,在这个日期范围内-

2014-12-03 - 10 users are active.
2014-12-04 - 10(active on 2014-12-03) + (new users for this date)
2014-12-05 - Number on active user on this date + all users in 15 day bucket 
2014-12-06 - Number on active user on this date + all users in 15 day bucket
查询:

SELECT CAST(`user_last_interaction_date` as Date)
FROM `users` U
    LEFT JOIN `user_source_history` USH on U.user_id = USH.user_id
WHERE  `user_last_interaction_date` > 
    (SELECT DATE_ADD(`user_last_interaction_date`, INTERVAL -15 DAY) 
     FROM `users`
     GROUP BY CAST(`user_last_interaction_date` as Date)
    )
GROUP BY CAST(`user_last_interaction_date` as Date)
我尝试了这个查询,但SQL说子查询返回多行


如何拆分查询以运行多行???

下面是一个示例查询,用于查找过去15天内处于活动状态的用户:

select  distinct name
from    users u
join    user_source_history uh
on      uh.user_id = u.user_id
where   user_last_interaction_date between
            now() and now() - interval 15 day

以下是从“2015-05-03”到“2015-05-18”期间没有活动用户的查询

您需要使用calendar\u date生成所有日期,它可以是数据库模式中的日期字段

SELECT c.calendar_date, count(*) active_users  
FROM `users` U 
LEFT JOIN `user_source_history` USH 
 on( U.user_id = USH.user_id )
CROSS JOIN (select * from
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) `calendar_date`  from
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where calendar_date between '2015-05-03' and '2015-05-18') as c 
on 
(date(USH.user_last_interaction_date) < c.calendar_date
and date(USH.user_last_interaction_date) >= (SELECT DATE_ADD(c.calendar_date, INTERVAL -15 DAY)))
group by c.calendar_date

我尝试了这个,但是这个查询从现在开始为我提供了活动用户。但我希望找到所有日期范围内的活动用户,即活动用户/天