sql server 2008中的apply運(yùn)算符使用方法
Apply運(yùn)算符可以實(shí)現(xiàn)兩個(gè)查詢結(jié)果的全組合結(jié)果,又稱為交叉集合。例如兩個(gè)數(shù)據(jù)組合(A,B)、(A,B),他們的交叉集合為(AA,AB,AA,AB)。
Apply分為Cross Apply和Outer Apply兩種使用方式。具體分析如下:
首先先建立兩個(gè)表StudentList和ScoreInfo。腳本語(yǔ)言如下:
create table StudentList(
id int Identity(1,1) not null,
Name nvarchar(20) not null,
Sex bit not null,
Birthday date not null,
Class nvarchar(2) not null,
Grade nvarchar(2) not null,
regdate date not null,
Primary key (id));
create table ScoreInfo(
id int Identity(1,1) not null primary key,
StudentID int not null,
ClassID int not null,
Score int not null,
TestDate date not null,
regdate date not null);
其中ScoreInfo中的StudentID為StudentList中id的外鍵
插入數(shù)據(jù),腳本如下
insert into StudentList(Name, Sex, Birthday, Class, Grade, regdate) values('張三', 1, '1988-05-28', 1, 8, '2010-05-05');
insert into StudentList(Name, Sex, Birthday, Class, Grade, regdate) values('李四', 1, '1985-09-13', 4, 4, '2010-05-05');
insert into StudentList(Name, Sex, Birthday, Class, Grade, regdate) values('王麗', 0, '1987-11-05', 1, 7, '2010-05-05');
insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(1, 1, 98, '2010-04-15', '2010-05-01');
insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(1, 2, 92, '2010-04-15', '2010-05-01');
insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(1, 3, 86, '2010-04-15', '2010-05-01');
insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(2, 1, 95, '2010-04-15', '2010-05-01');
insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(2, 2, 94, '2010-04-15', '2010-05-01');
insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(2, 3, 91, '2010-04-15', '2010-05-01');
insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(3, 1, 90, '2010-04-15', '2010-05-01');
insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(3, 2, 88, '2010-04-15', '2010-05-01');
insert into ScoreInfo(StudentID, ClassID, Score, TestDate, regdate) values(3, 3, 90, '2010-04-15', '2010-05-01');
兩個(gè)表結(jié)構(gòu)建立完畢,數(shù)據(jù)也成功插入進(jìn)去了。為了便于講解在StudentList表中再插入一條記錄
insert into StudentList(Name, Sex, Birthday, Class, Grade, regdate)
values('李銘', 1, '1989-05-04', 2, 7, '2010-05-05');
輸入以下語(yǔ)句
select * from StudentList a
cross apply
(select ClassID, Score from ScoreInfo where StudentID=a.id) b;
結(jié)果如下

再輸入以下語(yǔ)句
select * from StudentList a
outer apply
(select ClassID, Score from ScoreInfo where StudentID=a.id) b;
結(jié)果如下

可以看出Cross Apply和Outer Apply的區(qū)別
Cross Apply把語(yǔ)句兩邊的兩個(gè)Select查詢結(jié)果進(jìn)行交叉配對(duì),將所有結(jié)果展示出來(lái)。Cross Apply查詢確保在查詢兩個(gè)子集數(shù)據(jù)的交集時(shí),只有有效信息的集合才被列出來(lái)。
OuterApply查詢是把兩個(gè)子集的所有組合列了出來(lái),不管數(shù)據(jù)是否有交叉,全部顯示要配對(duì)的數(shù)據(jù)。
相關(guān)文章
SQL server 2008不允許保存更改的完美解決辦法(圖解)
我重裝系統(tǒng)后就安裝了SQL Server2008R2,第一次使用時(shí)在修改表結(jié)構(gòu)的時(shí)候經(jīng)碰到這樣一個(gè)警告【不允許保存更改。您所做的更改要求刪除并重新創(chuàng)建以下表.對(duì)這樣的錯(cuò)誤提示怎么解決呢?下面小編通過(guò)圖文并茂的形式給大家分享解決辦法2017-01-01
SQL Server 2008用''sa''登錄失敗,啟用''sa''登錄的解決辦法
這篇文章主要介紹了SQL Server 2008用'sa'登錄失敗,啟用'sa'登錄的解決辦法,感興趣的小伙伴們可以參考一下2015-09-09
解決SQL SERVER 2008數(shù)據(jù)庫(kù)表中修改字段后不能保存
SQL SERVER 2008數(shù)據(jù)庫(kù)表中修改字段后不能保存,這種情況將阻止保存要求重新創(chuàng)建表的更改一項(xiàng)的鉤鉤去掉就OK了2013-11-11
SQL2008中SQL應(yīng)用之- 死鎖(Deadlocking)
當(dāng)一個(gè)用戶會(huì)話(會(huì)話1)已經(jīng)落定了一個(gè)資源,而另一個(gè)會(huì)話(會(huì)話2)想要修改該資源,并且會(huì)話2也鎖定了會(huì)話1想要修改的資源時(shí),就會(huì)出現(xiàn)“死鎖”(deadlocking)。2011-06-06
windows系統(tǒng)下SQL?Server?2008超詳細(xì)安裝教程
有很多同學(xué)不知道怎么安裝SQL Server 2008,今天我就在這里教大家怎么安裝SQL Server 2008,下面這篇文章主要給大家介紹了關(guān)于windows系統(tǒng)下SQL?Server?2008超詳細(xì)安裝教程的相關(guān)資料,需要的朋友可以參考下2022-12-12
SQLServer 2008中SQL增強(qiáng)之三 Merge(在一條語(yǔ)句中使用Insert,Update,Delete)
SQLServer 2008中SQL增強(qiáng)之三 Merge(在一條語(yǔ)句中使用Insert,Update,Delete)2011-05-05
SQL Server 2008數(shù)據(jù)庫(kù)分布式查詢知識(shí)
今天小編就為大家分享一篇關(guān)于SQL Server 2008數(shù)據(jù)庫(kù)分布式查詢知識(shí),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01
SQLServer2008存儲(chǔ)過(guò)程實(shí)現(xiàn)數(shù)據(jù)插入與更新
這篇文章主要介紹了SQLServer2008下存儲(chǔ)過(guò)程實(shí)現(xiàn)數(shù)據(jù)插入與更新,需要的朋友可以參考下2018-08-08
SQLSERVER2008中CTE的Split與CLR的性能比較
之前曾有一篇POST是關(guān)于用CTE實(shí)現(xiàn)Split,這種方法已經(jīng)比傳統(tǒng)的方法高效了。今天我們就這個(gè)方法與CLR實(shí)現(xiàn)的Split做比較。在CLR實(shí)現(xiàn)Split函數(shù)的確很簡(jiǎn)單,dotnet framework本身就有這個(gè)function了。2011-10-10

