mysql 如何实现两个表数据统计合并
表一为用户表,包括id,名字和注册时间表二为登录表,包括登录时间,登录的用户id.表三是想要实现的结果:按时间统计合并一天的注册数和登录数。数据库大神,请多多指教!不胜感...
表一为用户表,包括id,名字和注册时间
表二为登录表,包括登录时间,登录的用户id.
表三是想要实现的结果:按时间统计合并一天的注册数和登录数。
数据库大神,请多多指教!不胜感激... 展开
表二为登录表,包括登录时间,登录的用户id.
表三是想要实现的结果:按时间统计合并一天的注册数和登录数。
数据库大神,请多多指教!不胜感激... 展开
展开全部
select a.time,sum(regTime) regSum,sum(logTime) logSum from
(select regTime time,1 regTime , 0 logSum from user union all select logTime time,0 regTime , 1 logSum from login) a group by a.time order by a.time;
------------------
select a.time,ifnull(b.regSum,0) regSum,ifnull(c.loginSum,0) loginSum from
(select regtime time from user union select logintime time from lgoin) a
left join (select regTime time,count(*) regSum from user group by regTime) b
on (a.time=b.time)
left join (select loginTime time,count(*) logSum from login group by loginTime) c
on (a.time=c.time)
order by a.time;
展开全部
直接一条语句实现起来很麻烦,可以把问题分成三步
select regTime as time, count(*) as regSum from user group by regTime; 这个当做表r
select time, count(*) as logSum from login group by time; 这个当做表l
第一步找出r,l的交集
select r.time as time , r.regSum as regSum, l.logSum as logSum
from r inner join l on r.time = l.time;
第二步找出r差l
select r.time as time , r.regSum as regSum, 0 as logSum
from r life join l on r.time = l.time
where l.time is null ;
第三步找出l差r
select l.time as time , 0 as regSum, l.logSum as logSum
from r right join l on r.time = l.time
where r.time is null ;
然后 union all 三步的结果。
可以写存储过程将r,l生成临时表,最后导出三步的结果。
select regTime as time, count(*) as regSum from user group by regTime; 这个当做表r
select time, count(*) as logSum from login group by time; 这个当做表l
第一步找出r,l的交集
select r.time as time , r.regSum as regSum, l.logSum as logSum
from r inner join l on r.time = l.time;
第二步找出r差l
select r.time as time , r.regSum as regSum, 0 as logSum
from r life join l on r.time = l.time
where l.time is null ;
第三步找出l差r
select l.time as time , 0 as regSum, l.logSum as logSum
from r right join l on r.time = l.time
where r.time is null ;
然后 union all 三步的结果。
可以写存储过程将r,l生成临时表,最后导出三步的结果。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询