关系数据库SQL中,union all语句具体用法?

 在关系数据库(如MySQL)中,有公司员工收入表t_income,包含字段:dept 部门、name员工姓名、income收入。现要统计每个员工的总收入、每个部门的总收入和全公司的总收入,可以用以下哪个SQL语句实现?(正确答案个数:2个)

A. select dept,name,sum(income) from t_income group by dept,name with cube;
B. select dept,name,sum(income) from t_income group by dept,name with rollup;
c. select dept,name,sum(income) from t_income group by dept,NAME union all select dept,",sum(income) from t_income group by dept union all select ", ",sum(income) from t_income;
D.select dept,name,sum(income) from t_income group by dept,NAME union all select dept,",sum(income) from t_income group by dept union all select ",name,sum(income) from t_income group by name union all select ",",sum(income) from t_income;

正确答案为B\C项

请帮忙解释一下为什么C项正确

B应该是很容易理解的

C主要是要了解union all是什么意思,union其实是把三条独立的SQL语句查询,然后把结果显示在一起。

这就是union的作用,只要结构相同就可以使用union实现独立SQL语句的结果显示在一起。

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632