[英]Combine query for sum of two tables with date
I have two tables like following 我有两个如下表
challenge_table 挑战表
Challenger chdate
----------------------
abc 11-02-2012
aaa 12-02-2012
ccc 12-02-2012
bbb 13-02-2012
ddd 14-02-2012
init_table
Initiateid indate
----------------------
a1 11-02-2012
a2 11-02-2012
a3 12-02-2012
a4 13-02-2012
a5 13-02-2012
I need a result like this 我需要这样的结果
challengecount initcount curdate
-----------------------------------
1 2 11-02-2012
2 1 12-02-2012
1 2 13-02-2012
1 0 14-02-2012
i tried a query like this 我试过这样的查询
SELECT COUNT(*) challengecount, chdate caldate FROM challenge_table
UNION ALL
SELECT COUNT(*) Initiatecount, indate caldate FROM init_table
But it doesn't work for me. 但这对我不起作用。
Since MySQL does not support a FULL OUTER JOIN, you need to combine the 2 tables with a UNION. 由于MySQL不支持FULL OUTER JOIN,因此您需要将2个表与UNION合并。
Then group by the date and count the values of the 2 tables 然后按日期分组并计算2个表的值
select caldate, count(distinct Challenger), count(distinct Initiateid)
from
(
SELECT Challenger, null as Initiateid, chdate as caldate FROM challenge_table
UNION ALL
SELECT null, Initiateid, indate FROM init_table
) tmp
group by caldate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.