我的查询
------------------------------------------
Select cat_name,cat,total
From
(
SELECT a.category_name as cat_name,
Count (a.category_name) as cat,
Sum( b.position) as total
FROM erom.category a, erom_kmch.nsdl b
WHERE a.bene_type_nsdl = b.bene_type
AND a.bene_stype_nsdl= b.bene_stype
And b.date ='2016-07-22'
Group By cat_name
UNION All
SELECT a.category_name as cat_name,
Count(a.category_name) as cat,
Sum( b.shares) as total
FROM erom.category a, erom_kmch.cdsl b
WHERE a.type_cdsl = b.type
AND a.bo_substat_cdsl= b.bo_substat
And b.date ='2016-07-22' group by cat_name
UNION All
SELECT a.category_name as cat_name,
Count(a.category_name) as cat,
Sum( b.shares) as total
FROM erom.category a, erom_kmch.member_member_master b
WHERE a.substatus_phy = b.substatus
And b.date ='2016-07-22'
And shares > '0'
Group By cat_name
)
c Group By cat_name
------------------------------------
返回
----------------------------
cat_name cat total
Resident Individual 2705 2317048
---------------------------------------
如果我分别运行三个表,我将得到输出
----------------------------------------
cat_name cat total
Resident Individual 2705 2317048
Resident Individual 991 355218
Resident Individual 3284 1219027
---------------------------------------
但我需要输出为
--------------------------
cat_name cat total
Resident Individual 6980 3891293
-----------------------------------
我正在尝试获取表中字段名称的总数,但我只得到第一个表的答案。帮助我找到解决方案。
最佳答案
您的查询的第一行更改为
select cat_name, sum(cat) as cat, sum(total) as total
关于mysql - 如何联合并获得三个表的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38866282/