具有连接输出的MYSQL计数

时间:2017-09-04 14:57:54

标签: mysql

我有2张桌子

Table 1
date         Tcount
2017-09-04   1
2017-09-04   1
2017-09-05   1
2017-09-03   1
Table 2
date         Tcount
2017-09-05   2
2017-09-02   1

如何使输出如下(与连接mysql或等)????


date         TcountTable1   TcountTable2
2017-09-02   null           1
2017-09-03   1              null
2017-09-04   2              null
2017-09-05   1              2

1 个答案:

答案 0 :(得分:0)

这相当于其他数据库中的full join(聚合后)。

MySQL不支持full join。您可以使用union all和聚合:

select date, sum(tcount1) as tcount1, sum(tcount2) as tcount2
from ((select date, tcount as tcount1, null as tcount2
       from table1
      ) union all
      (select date, NULL as tcount1, tcount as tcount2
       from table2
      )
     ) tt
group by date;