MySQL计算来自不同的表

时间:2014-11-12 07:35:25

标签: mysql count

表A

Status
1
1
1
2
2
3

表B

Status
1
1
2
2
2
2
3
3

如何获取以下结果表

结果表

Status    Count
1           5
2           6
3           3

帮助我构建SQL查询以获取结果表中的计数。

2 个答案:

答案 0 :(得分:1)

Try this

SELECT Status, COUNT(*) FROM
(SELECT a.Status FROM TableA AS a
UNION ALL
SELECT b.Status FROM TableB AS b) UN
GROUP BY Status

答案 1 :(得分:0)

SELECT STATUS, COUNT(*) FROM
(SELECT STATUS FROM TABLE1
UNION ALL 
SELECT STATUS FROM TABLE2) un
GROUP BY STATUS

FIDDLE