我有以下4个相同的选择,返回相同的列输出,结果不同。
类似的东西:
1)select a, count(*) from table a;
2)select a, count(*) from table b;
3)select a, count(*) from table c;
4)select a, count(*) from table d;
他们都输出如下内容:
=================
a |count(*) (Table a)|
=================
hi | 2
hello | 3
why | 4
=================
a |count(*) (Table b)|
=================
hi | 4
hello | 6
why | 8
类似于表c和d。
我想在所有这四个表上进行连接,以便我可以获得一个表,其中相同的列被求和并分组并显示为一个,像这样的东西
=================
a |count(*) (Table a & b union )|
=================
hi | 6
hello | 9
why | 12
我已经找到了一个适用于两个表的查询,但是桌面上的其他联合不能很好地运行:
select a, sum(total) from (select a, count(*) from table a union all select a, count(*) from table b) as T group by a;
这适用于两张桌子但不超过我错过的内容。
答案 0 :(得分:1)
select a,
sum(total) from
(select a, count(*) AS total from table a
union all
select a, count(*) AS total from table b
union all
select a, count(*) AS total from table c
union all
select a, count(*) AS total from table d
union all
)
group by a
答案 1 :(得分:0)
您需要在执行以下计算之前完成工作
select a, count(*) from
(
select a from table a
union all
select a from table b
union all
select a from table c
union all
select a from table d
) as T
group by a