亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

帶例子詳解Sql中Union和Union?ALL的區(qū)別

 更新時間:2022年09月25日 11:22:42   作者:我贏了算我輸  
這篇文章主要介紹了帶例子詳解Sql中Union和Union?ALL的區(qū)別,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下

前言

一段時間沒有用Union和Union,再用的時候忘了怎么用了。。。所以做一篇文章來記錄自己學Union和Union的經歷。

提前準備

在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,'學生A',13)
insert into Student1 values(2,'學生B',13)
insert into Student1 values(3,'學生C',13)
insert into Student1 values(4,'學生D',13)

insert into Student2 values(1,'學生A',13)
insert into Student2 values(2,'學生E',13)
insert into Student2 values(3,'學生F',13)
insert into Student2 values(4,'學生D',13)

創(chuàng)建兩張一摸一樣的表并插入數據。

測試

UNION:合并兩個或三個以上的Select語句的結果集,合并之后的結果集不包含重復的數

UNION ALL:合并兩個或三個以上的Select語句的結果集,合并之后的結果集可以包含重復的數 。

可以看出,Union和Union All 的區(qū)別在于是否“包含重復數”,而重復數又指的是什么呢?當出現一條數據與另一條數據的所有列數據完全相同的情況,那么就稱這條數據為重復數,下面拿例子來演示

Union

select * from Student1
union
select * from Student2

結果:

id為2、3的出現了兩條,是因為Student1 id為2的Name列數據和Student2 id為2的Name列數據不同 (Student1為學生B,Student為學生E),并不能算重復數。
id為1、4、5只出現一次,是因為Student1和Student2中id為1、4、5的列數據完全相同,所以算重復數

Union ALL

select * from Student1
union ALL
select * from Student2

union all 就無需顧及到重復數,直接把兩個表的結果集合并一起展示就OK了。
除此之外,還有幾種情況

1.當表Student1和表Student2字段數量不同的情況下,使用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,'學生A',13)
insert into Student3 values(2,'學生B',13)
insert into Student3 values(3,'學生C',13)
insert into Student3 values(4,'學生D',13)

insert into Student4 values(1,'學生A')
insert into Student4 values(2,'學生E')
insert into Student4 values(3,'學生F')
insert into Student4 values(4,'學生D')

Student3 和Student4的字段不一樣,Student3表比Student4表多一個Age字段

Union

select * from Student3
union
select * from Student4

Union All

select * from Student3
union ALL
select * from Student4

測試結果顯示:只有當涉及的幾個表的列具有相同的數量,才能使用Union和UnionALL

2.當表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,'學生A',13)
insert into Student3 values(2,'學生B',13)
insert into Student3 values(3,'學生C',13)
insert into Student3 values(4,'學生D',13)

insert into Student4 values(1,'學生A',13)
insert into Student4 values(2,'學生B',13)
insert into Student4 values(3,'學生C',13)
insert into Student4 values(4,'學生D',13)

這里Student3的第二個欄位是NameTest,而Student4第二個欄位是Name,Student3和Student4的數據一樣。

union

select * from Student3
union 
select * from Student4

Union All

select * from Student3
union  ALL
select * from Student4

測試結果顯示:如果當列數量一樣,列名不相同,那么列名優(yōu)先顯示先執(zhí)行Select語句的結果集的列名

最后

上面進行了對 “重復數”、“列數不同”、“列數相同,但列名不相同”進行測試,我得出了自己認為的答案

到此這篇關于帶例子詳解Sql中Union和Union ALL的區(qū)別的文章就介紹到這了,更多相關Sql Union和Union ALL內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • MySQL連接異常報10061錯誤問題解決

    MySQL連接異常報10061錯誤問題解決

    這篇文章主要介紹了MySQL連接異常報10061錯誤問題解決,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-08-08
  • MySQL5.7主從復制詳細配置教程

    MySQL5.7主從復制詳細配置教程

    這篇文章主要介紹了MySQL5.7主從復制詳細配置教程的相關資料,需要的朋友可以參考下
    2022-11-11
  • Mysql數據庫設計三范式實例解析

    Mysql數據庫設計三范式實例解析

    這篇文章主要介紹了Mysql數據庫設計三范式實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • Mac 下 MySQL5.7.22的安裝過程

    Mac 下 MySQL5.7.22的安裝過程

    這篇文章主要介紹了Mac 下 MySQL5.7.22的安裝過程,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2018-05-05
  • MySQL參數優(yōu)化信息參考(my.cnf參數優(yōu)化)

    MySQL參數優(yōu)化信息參考(my.cnf參數優(yōu)化)

    下面針對一些參數進行說明,當然還有其它的設置可以起作用,取決于你的負載或硬件:在慢內存和快磁盤、高并發(fā)和寫密集型負載情況下,你將需要特殊的調整
    2024-07-07
  • Mysql如何查詢字符串開頭的數據

    Mysql如何查詢字符串開頭的數據

    這篇文章主要介紹了Mysql如何查詢字符串開頭的數據問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 常見php與mysql中文亂碼問題解決辦法

    常見php與mysql中文亂碼問題解決辦法

    MySQL對中文的支持程度還是很有限的,尤其是新手,但凡出現亂碼問題,就會頭大。
    2014-09-09
  • MYSQL??group?by?有哪些注意事項

    MYSQL??group?by?有哪些注意事項

    這篇文章主要介紹了MYSQL??group?by?有哪些注意事項,比如我們不能在?group?by?之后添加?where?查詢語句,更多相關分享,需要的朋友可以參考下面文章內容
    2022-07-07
  • MySQL中執(zhí)行計劃explain命令示例詳解

    MySQL中執(zhí)行計劃explain命令示例詳解

    這篇文章主要給大家介紹了關于MySQL中執(zhí)行計劃explain命令的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用explain命令具有一定的參考學習價值,需要的朋友們下面說來一起學習學習吧
    2018-05-05
  • MySQL優(yōu)化中B樹索引知識點總結

    MySQL優(yōu)化中B樹索引知識點總結

    在本文里我們給大家整理了關于MySQL優(yōu)化中B樹索引的相關知識點內容,需要的朋友們可以學習下。
    2019-02-02

最新評論