帶例子詳解Sql中Union和Union?ALL的區(qū)別
前言
一段時間沒有用Union和Union,再用的時候忘了怎么用了。。。所以做一篇文章來記錄自己學(xué)Union和Union的經(jīng)歷。
提前準(zhǔn)備
在Sql Server 創(chuàng)建兩張表,下面是創(chuàng)建表sql語句。
create table Student1( Id varchar(50) not null, Name varchar(50) not null, Age int not null ) create table Student2( Id varchar(50) not null, Name varchar(50) not null, Age int not null )
insert into Student1 values(1,'學(xué)生A',13) insert into Student1 values(2,'學(xué)生B',13) insert into Student1 values(3,'學(xué)生C',13) insert into Student1 values(4,'學(xué)生D',13) insert into Student2 values(1,'學(xué)生A',13) insert into Student2 values(2,'學(xué)生E',13) insert into Student2 values(3,'學(xué)生F',13) insert into Student2 values(4,'學(xué)生D',13)
創(chuàng)建兩張一摸一樣的表并插入數(shù)據(jù)。
測試
UNION:合并兩個或三個以上的Select語句的結(jié)果集,合并之后的結(jié)果集不包含重復(fù)的數(shù)
UNION ALL:合并兩個或三個以上的Select語句的結(jié)果集,合并之后的結(jié)果集可以包含重復(fù)的數(shù) 。
可以看出,Union和Union All 的區(qū)別在于是否“包含重復(fù)數(shù)”,而重復(fù)數(shù)又指的是什么呢?當(dāng)出現(xiàn)一條數(shù)據(jù)與另一條數(shù)據(jù)的所有列數(shù)據(jù)完全相同的情況,那么就稱這條數(shù)據(jù)為重復(fù)數(shù),下面拿例子來演示
Union
select * from Student1 union select * from Student2
結(jié)果:
id為2、3的出現(xiàn)了兩條,是因?yàn)镾tudent1 id為2的Name列數(shù)據(jù)和Student2 id為2的Name列數(shù)據(jù)不同 (Student1為學(xué)生B,Student為學(xué)生E),并不能算重復(fù)數(shù)。
id為1、4、5只出現(xiàn)一次,是因?yàn)镾tudent1和Student2中id為1、4、5的列數(shù)據(jù)完全相同,所以算重復(fù)數(shù)。
Union ALL
select * from Student1 union ALL select * from Student2
union all 就無需顧及到重復(fù)數(shù),直接把兩個表的結(jié)果集合并一起展示就OK了。
除此之外,還有幾種情況
1.當(dāng)表Student1和表Student2字段數(shù)量不同的情況下,使用Union和Union ALL
create table Student3( Id varchar(50) not null, Name varchar(50) not null, Age int not null ) create table Student4( Id varchar(50) not null, Name varchar(50) not null ) insert into Student3 values(1,'學(xué)生A',13) insert into Student3 values(2,'學(xué)生B',13) insert into Student3 values(3,'學(xué)生C',13) insert into Student3 values(4,'學(xué)生D',13) insert into Student4 values(1,'學(xué)生A') insert into Student4 values(2,'學(xué)生E') insert into Student4 values(3,'學(xué)生F') insert into Student4 values(4,'學(xué)生D')
Student3 和Student4的字段不一樣,Student3表比Student4表多一個Age字段
Union
select * from Student3 union select * from Student4
Union All
select * from Student3 union ALL select * from Student4
測試結(jié)果顯示:只有當(dāng)涉及的幾個表的列具有相同的數(shù)量,才能使用Union和UnionALL
2.當(dāng)表A和表B的列相同,但是列名不一致的情況,使用Union和Union ALL
create table Student3( Id varchar(50) not null, NameTest varchar(50) not null, Age int not null ) create table Student4( Id varchar(50) not null, Name varchar(50) not null, Age int not null ) insert into Student3 values(1,'學(xué)生A',13) insert into Student3 values(2,'學(xué)生B',13) insert into Student3 values(3,'學(xué)生C',13) insert into Student3 values(4,'學(xué)生D',13) insert into Student4 values(1,'學(xué)生A',13) insert into Student4 values(2,'學(xué)生B',13) insert into Student4 values(3,'學(xué)生C',13) insert into Student4 values(4,'學(xué)生D',13)
這里Student3的第二個欄位是NameTest,而Student4第二個欄位是Name,Student3和Student4的數(shù)據(jù)一樣。
union
select * from Student3 union select * from Student4
Union All
select * from Student3 union ALL select * from Student4
測試結(jié)果顯示:如果當(dāng)列數(shù)量一樣,列名不相同,那么列名優(yōu)先顯示先執(zhí)行Select語句的結(jié)果集的列名
最后
上面進(jìn)行了對 “重復(fù)數(shù)”、“列數(shù)不同”、“列數(shù)相同,但列名不相同”進(jìn)行測試,我得出了自己認(rèn)為的答案
到此這篇關(guān)于帶例子詳解Sql中Union和Union ALL的區(qū)別的文章就介紹到這了,更多相關(guān)Sql Union和Union ALL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MySQL中關(guān)鍵字UNION和UNION ALL的區(qū)別
- MySQL之union和union all的使用及區(qū)別說明
- 簡單聊一聊SQL中的union和union?all
- MySQL系列理解運(yùn)用union(all)與limit及exists關(guān)鍵字教程
- 簡單了解MySQL union all與union的區(qū)別
- MySQL中UNION與UNION ALL的基本使用方法
- 淺析mysql union和union all
- 5分鐘了解MySQL5.7中union all用法的黑科技
- SQL語句之Union和Union All的用法
- SQL中UNION與UNION ALL的區(qū)別小結(jié)
相關(guān)文章
Mysql數(shù)據(jù)庫設(shè)計三范式實(shí)例解析
這篇文章主要介紹了Mysql數(shù)據(jù)庫設(shè)計三范式實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04MySQL參數(shù)優(yōu)化信息參考(my.cnf參數(shù)優(yōu)化)
下面針對一些參數(shù)進(jìn)行說明,當(dāng)然還有其它的設(shè)置可以起作用,取決于你的負(fù)載或硬件:在慢內(nèi)存和快磁盤、高并發(fā)和寫密集型負(fù)載情況下,你將需要特殊的調(diào)整2024-07-07MYSQL??group?by?有哪些注意事項(xiàng)
這篇文章主要介紹了MYSQL??group?by?有哪些注意事項(xiàng),比如我們不能在?group?by?之后添加?where?查詢語句,更多相關(guān)分享,需要的朋友可以參考下面文章內(nèi)容2022-07-07MySQL優(yōu)化中B樹索引知識點(diǎn)總結(jié)
在本文里我們給大家整理了關(guān)于MySQL優(yōu)化中B樹索引的相關(guān)知識點(diǎn)內(nèi)容,需要的朋友們可以學(xué)習(xí)下。2019-02-02