生活随笔
收集整理的這篇文章主要介紹了
sql中union和union all的区别
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
union 連接兩個表查詢的結(jié)果
假設(shè)我們有一個表Student,包括以下字段與數(shù)據(jù):
?
[c-sharp] view plaincopy
drop?table?student;????create?table?student??(??id?int?primary?key,??name?nvarchar2(50)?not?null,??score?number?not?null??);????insert?into?student?values(1,'Aaron',78);??insert?into?student?values(2,'Bill',76);??insert?into?student?values(3,'Cindy',89);??insert?into?student?values(4,'Damon',90);??insert?into?student?values(5,'Ella',73);??insert?into?student?values(6,'Frado',61);??insert?into?student?values(7,'Gill',99);??insert?into?student?values(8,'Hellen',56);??insert?into?student?values(9,'Ivan',93);??insert?into?student?values(10,'Jay',90);????commit;?? ?
首先,我們來看一下UNION的例子:
[c-sharp] view plaincopy
SQL>?select?*????2??from?student????3??where?id<4????4??union????5??select?*????6??from?student????7??where?id>2?and?id<6????8??;????????????ID?NAME????????????????????????????????SCORE??----------?------------------------------?----------???????????1?Aaron??????????????????????????????????78???????????2?Bill???????????????????????????????????76???????????3?Cindy??????????????????????????????????89???????????4?Damon??????????????????????????????????90???????????5?Ella???????????????????????????????????73????SQL>?? ?
如果換成Union All連接兩個結(jié)果集,則結(jié)果如下:
?
[c-sharp] view plaincopy
SQL>?select?*????2??from?student????3??where?id<4????4??union?all????5??select?*????6??from?student????7??where?id>2?and?id<6????8??;????????????ID?NAME????????????????????????????????SCORE??----------?------------------------------?----------???????????1?Aaron??????????????????????????????????78???????????2?Bill???????????????????????????????????76???????????3?Cindy??????????????????????????????????89???????????3?Cindy??????????????????????????????????89???????????4?Damon??????????????????????????????????90???????????5?Ella???????????????????????????????????73????6?rows?selected.?? ?
可以看到,Union和Union All的區(qū)別之一在于對重復(fù)結(jié)果的處理。
?
接下來,我們交換一個兩個SELECT語句的順序,看看結(jié)果是怎樣的。
?
[c-sharp] view plaincopy
SQL>?select?*????2??from?student????3??where?id>2?and?id<6????4??union????5??select?*????6??from?student????7??where?id<4????8??;????????????ID?NAME????????????????????????????????SCORE??----------?------------------------------?----------???????????1?Aaron??????????????????????????????????78???????????2?Bill???????????????????????????????????76???????????3?Cindy??????????????????????????????????89???????????4?Damon??????????????????????????????????90???????????5?Ella???????????????????????????????????73????SQL>?select?*????2??from?student????3??where?id>2?and?id<6????4??union?all????5??select?*????6??from?student????7??where?id<4????8??;????????????ID?NAME????????????????????????????????SCORE??----------?------------------------------?----------???????????3?Cindy??????????????????????????????????89???????????4?Damon??????????????????????????????????90???????????5?Ella???????????????????????????????????73???????????1?Aaron??????????????????????????????????78???????????2?Bill???????????????????????????????????76???????????3?Cindy??????????????????????????????????89????6?rows?selected.?? ?
可以看到,對于UNION來說,交換兩個SELECT語句的順序后結(jié)果仍然是一樣的,這是因為UNION會自動排序。而UNION ALL在交換了SELECT語句的順序后結(jié)果則不相同,因為UNION ALL不會對結(jié)果自動進行排序。
?
那么這個自動排序的規(guī)則是什么呢?我們交換一下SELECT后面選擇字段的順序(前面使用SELECT *相當(dāng)于SELECT ID,NAME,SCORE),看看結(jié)果如何:
?
[c-sharp] view plaincopy
SQL>?select?score,id,name????2??from?student????3??where?id<4????4??union????5??select?score,id,name????6??from?student????7??where?id>2?and?id<6????8??;?????????SCORE?????????ID?NAME??----------?----------?------------------------------??????????73??????????5?Ella??????????76??????????2?Bill??????????78??????????1?Aaron??????????89??????????3?Cindy??????????90??????????4?Damon?? ?
可是看到,此時是按照字段SCORE來對結(jié)果進行排序的(前面SELECT *的時候是按照ID進行排序的)。
?
那么有人會問,如果我想自行控制排序,能不能使用ORDER BY呢?當(dāng)然可以。不過在寫法上有需要注意的地方:
?
[c-sharp] view plaincopy
select?score,id,name??from?student??where?id?>?2?and?id?<?7????union????select?score,id,name??from?student??where?id?<?4????union????select?score,id,name??from?student??where?id?>?8??order?by?id?desc?? ?
order by子句必須寫在最后一個結(jié)果集里,并且其排序規(guī)則將改變操作后的排序結(jié)果。對于Union、Union All、Intersect、Minus都有效。
?
其他的集合操作符,如Intersect和Minus的操作和Union基本一致,這里一起總結(jié)一下:
?
Union,對兩個結(jié)果集進行并集操作,不包括重復(fù)行,同時進行默認規(guī)則的排序;
Union All,對兩個結(jié)果集進行并集操作,包括重復(fù)行,不進行排序;
Intersect,對兩個結(jié)果集進行交集操作,不包括重復(fù)行,同時進行默認規(guī)則的排序;
Minus,對兩個結(jié)果集進行差操作,不包括重復(fù)行,同時進行默認規(guī)則的排序。
?
可以在最后一個結(jié)果集中指定Order by子句改變排序方式。
轉(zhuǎn)載于:https://www.cnblogs.com/chyg/archive/2012/11/24/2785424.html
總結(jié)
以上是生活随笔為你收集整理的sql中union和union all的区别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。