1个回答
展开全部
联合查询+子查询【注意要联合各个表的字段名和数量都要一样】:
SELECT 号码, 部门, SUM(费用1) AS 费用1, SUM(费用2) AS 费用2, SUM(费用3) AS 费用3 FROM (
SELECT 号码, 部门, 费用1, 费用2, 费用3 FROM 表1
UNION ALL
SELECT 号码, 部门, 费用1, 费用2, 费用3 FROM 表2
UNION ALL
SELECT 号码, 部门, 费用1, 费用2, 0 AS 费用3 FROM 表3
UNION ALL
SELECT 号码, 部门, 0 AS 费用1, 0 AS 费用2, 费用3 FROM 表4) AS TEMPTABLE
GROUP BY 号码, 部门;
SELECT 号码, 部门, SUM(费用1) AS 费用1, SUM(费用2) AS 费用2, SUM(费用3) AS 费用3 FROM (
SELECT 号码, 部门, 费用1, 费用2, 费用3 FROM 表1
UNION ALL
SELECT 号码, 部门, 费用1, 费用2, 费用3 FROM 表2
UNION ALL
SELECT 号码, 部门, 费用1, 费用2, 0 AS 费用3 FROM 表3
UNION ALL
SELECT 号码, 部门, 0 AS 费用1, 0 AS 费用2, 费用3 FROM 表4) AS TEMPTABLE
GROUP BY 号码, 部门;
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询