在SQL Server 2008数据库中,我们常常会使用到联合结果集union,本文我们主要介绍了一个union的使用实例,希望能够对您有所帮助。
联合结果集的使用如下:
新建临时工数据表
class="dp-xml">
- CREATE TABLE T_TempEmployee (FIdCardNumber VARCHAR(20),FName VARCHAR(20),FAge int,PRIMARY KEY(FIdCardNumber));
- INSERT INTO T_TempEmployee(FIdCardNumber,FName,FAge) VALUES('1234567890121','Sarani',33);
- INSERT INTO T_TempEmployee(FIdCardNumber,FName,FAge) VALUES('1234567890122','Tom',26);
- INSERT INTO T_TempEmployee(FIdCardNumber,FName,FAge) VALUES('1234567890123','Yalaha',38);
- INSERT INTO T_TempEmployee(FIdCardNumber,FName,FAge) VALUES('1234567890124','Tina',26);
- INSERT INTO T_TempEmployee(FIdCardNumber,FName,FAge) VALUES('1234567890125','Konkaya',29);
- INSERT INTO T_TempEmployee(FIdCardNumber,FName,FAge) VALUES('1234567890126','Fotifa',46);
Union联合
- select FNumber,FAge from T_Employee
- union
- select FIdCardNumber,FAge from T_TempEmployee
union上下的查询个数和类型必须对应一致
- select FNumber,FAge,FDepartment from T_Employee
- union
- select FIdCardNumber,FAge ,'临时工,无部门' from T_TempEmployee
union默认将重复数据去掉。如果不让他去掉,则用union all
- select FName from T_Employee
- union all
- select FName from T_TempEmployee
关于SQL Server 2008学习笔记之联合结果集union的使用的相关知识就介绍这里了,希望本次的介绍能够对您有所收获!