联合结果集union

--联合结果集union
go
create view vw_test
as
select * from Student
union all
select * from Student
go
select StudentNo,StudentName from Student
union
select classid,classname from Classes
--union就是用来合并多个结果集的。
--1.运算符合并的所有查询必须在其目标列表中有相同数目的表达式
--2.合并的结果集对应的列的类型需要一致
select StudentName,StudentNo from Student
union
select classid,classname from Classes
--union---union all
--union:去除重复记录。因为需要做是否重复的判断及去除重复的操作,所有性能会下降
--union all:不去除重复记录

--查询学员的成员,显示学号和科目ID,及成绩,在最后显示学员的平均分
select cast(StudentNo as char(3)),SubjectId,StudentResult from Result
union --联合的时候不会为前面的结果集排序,只能在最后进行排序
select ' 平均分','',cast(AVG(StudentResult) as CHAR(3)) from Result order by StudentResult

--一次插入多条数据--
insert into 表 values(值列表) ---一次性只能插入一条记录
--使用union插入多条记录,不能写Default 都使用union all才不去除重复
insert into Classes
select 'aa' union all
select 'aa' union all
select 'aa' union all
select 'aa'

人的本事不是与生俱来的,不是你掌握了多少,而是当你面对一个未知问题的时候,你能用多少时间来掌握!
原文地址:https://www.cnblogs.com/dianshen520/p/4351944.html