2016-09-12 79 views
0

我有兩個表像下組合查詢兩個表的總和與日期

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 

我需要這樣的

challengecount initcount curdate 
----------------------------------- 
1    2   11-02-2012 
2    1   12-02-2012 
1    2   13-02-2012 
1    0   14-02-2012 

結果我嘗試了這樣的查詢

SELECT COUNT(*) challengecount, chdate caldate FROM challenge_table 
UNION ALL 
SELECT COUNT(*) Initiatecount, indate caldate FROM init_table 

Bu它不適合我。

+0

你與你的查詢會得到什麼結果的價值? – UmNyobe

回答

0

由於MySQL不支持FULL OUTER JOIN,因此您需要將2個表與UNION結合使用。

然後按日期組,算上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