SQL学习笔记六 union联合结果集使用

前端之家收集整理的这篇文章主要介绍了SQL学习笔记六 union联合结果集使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

联合结果集
新建临时工数据表
<div class="codetitle"><a style="CURSOR: pointer" data="91779" class="copybut" id="copybut91779" onclick="doCopy('code91779')"> 代码如下:

<div class="codebody" id="code91779">
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,FAge) VALUES('1234567890122','Tom',26);
INSERT INTO T_TempEmployee(FIdCardNumber,FAge) VALUES('1234567890123','Yalaha',38);
INSERT INTO T_TempEmployee(FIdCardNumber,FAge) VALUES('1234567890124','Tina',FAge) VALUES('1234567890125','Konkaya',29);
INSERT INTO T_TempEmployee(FIdCardNumber,FAge) VALUES('1234567890126','Fotifa',46);
Union联合
<div class="codetitle"><a style="CURSOR: pointer" data="83493" class="copybut" id="copybut83493" onclick="doCopy('code83493')"> 代码如下:
<div class="codebody" id="code83493">
select FNumber,FAge from T_Employee
union
select FIdCardNumber,FAge from T_TempEmployee
union上下的查询个数和类型必须对应一致
<div class="codetitle"><a style="CURSOR: pointer" data="61467" class="copybut" id="copybut61467" onclick="doCopy('code61467')"> 代码如下:
<div class="codebody" id="code61467">
select FNumber,FAge,FDepartment from T_Employee
union
select FIdCardNumber,'临时工,无部门' from T_TempEmployee
union默认将重复数据去掉。如果不让他去掉,则用union all
<div class="codetitle"><a style="CURSOR: pointer" data="988" class="copybut" id="copybut988" onclick="doCopy('code988')"> 代码如下:
<div class="codebody" id="code988">
select FName from T_Employee
union all
select FName from T_TempEmployee

原文链接:https://www.f2er.com/mssql/63982.html
联合结果集

猜你在找的MsSQL相关文章