ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

SQL语句UNION和union all的用法

2021-11-13 10:29:59  阅读:184  来源: 互联网

标签:salary name UNION scholor union student SQL test id


SQL语句UNION和union all的用法

主要说UNION ALL

功能:

将两个流式数据合并
注意:
		两个流式数据的字段必须完全一致,包括字段类型和字段顺序。

union和union all的异同点

UNION ALL允许重复值,UNION不允许重复值。

在实时计算Flink版系统中,UNION相当于UNION ALL+Distinct,运行效率低,通常不推荐使用UNION。

测试数据

1.创建示例表(SQLSERVER)

创建test_u1

CREATE TABLE test_u1(
	  --主键自增:identity(起始值,增量)
      id int identity (1,1) primary key
	  ,name nvarchar(100) null
	  ,student_id nvarchar(100) null
	  ,scholor nvarchar(100) null
	  ,salary int
)

insert into test_u1(name,student_id,scholor,salary) values('boyunv01','20211113','primary','100');
insert into test_u1(name,student_id,scholor,salary) values('boyunv02','20211114','junior','200');
--insert into test_u1(name,student_id,scholor,salary) values('boyunv03','20211115','senior','500');
--insert into test_u1(name,student_id,scholor,salary) values('boyunv04','20211116','college','9000');
--insert into test_u1(name,student_id,scholor,salary) values('boyunv05','20211117','master','30000');
--insert into test_u1(name,student_id,scholor,salary) values('boyunv06','20211118','doctor','60000');
select  * from  test_u1  

在这里插入图片描述
创建test_u2和insert语句


CREATE TABLE test_u2(
	  --主键自增:identity(起始值,增量)
      id int identity (1,1) primary key
	  ,name nvarchar(100) null
	  ,student_id nvarchar(100) null
	  ,scholor nvarchar(100) null
	  ,salary int
)
insert into test_u2(name,student_id,scholor,salary) values('boyunv01','20211113','primary','100');
insert into test_u2(name,student_id,scholor,salary) values('boyunv02','20211114','junior','200');
insert into test_u2(name,student_id,scholor,salary) values('boyunv03','20211115','senior','500');
insert into test_u2(name,student_id,scholor,salary) values('boyunv04','20211116','college','9000');
--insert into test_u2(name,student_id,scholor,salary) values('boyunv05','20211117','master','30000');
--insert into test_u2(name,student_id,scholor,salary) values('boyunv06','20211118','doctor','60000');
select  * from  test_u2

在这里插入图片描述
创建test_u3和insert语句


CREATE TABLE test_u3(
	  --主键自增:identity(起始值,增量)
      id int identity (1,1) primary key
	  ,name nvarchar(100) null
	  ,student_id nvarchar(100) null
	  ,scholor nvarchar(100) null
	  ,salary int
)

insert into test_u3(name,student_id,scholor,salary) values('boyunv01','20211113','primary','100');
insert into test_u3(name,student_id,scholor,salary) values('boyunv02','20211114','junior','200');
insert into test_u3(name,student_id,scholor,salary) values('boyunv03','20211115','senior','500');
insert into test_u3(name,student_id,scholor,salary) values('boyunv04','20211116','college','9000');
insert into test_u3(name,student_id,scholor,salary) values('boyunv05','20211117','master','30000');
insert into test_u3(name,student_id,scholor,salary) values('boyunv06','20211118','doctor','60000');
select  * from  test_u3

在这里插入图片描述

执行union all 语句

select  
select  
	name,id,salary, sum(salary) as salary
	from (
	  SELECT * FROM test_u1 
	  UNION ALL
	  SELECT *  FROM  test_u2
	  UNION ALL
	  SELECT * FROM test_u3
	
	)T
	GROUP BY id,name,salary

执行结果
在这里插入图片描述

后续补上这后面解释我自己不咋信服~勿喷
在这里插入图片描述

标签:salary,name,UNION,scholor,union,student,SQL,test,id
来源: https://blog.csdn.net/weixin_43475992/article/details/121300217

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有