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。腳本語言如下:
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');
輸入以下語句
select * from StudentList a
cross apply
(select ClassID, Score from ScoreInfo where StudentID=a.id) b;
結(jié)果如下
再輸入以下語句
select * from StudentList a
outer apply
(select ClassID, Score from ScoreInfo where StudentID=a.id) b;
結(jié)果如下
可以看出Cross Apply和Outer Apply的區(qū)別
Cross Apply把語句兩邊的兩個(gè)Select查詢結(jié)果進(jìn)行交叉配對(duì),將所有結(jié)果展示出來。Cross Apply查詢確保在查詢兩個(gè)子集數(shù)據(jù)的交集時(shí),只有有效信息的集合才被列出來。
OuterApply查詢是把兩個(gè)子集的所有組合列了出來,不管數(shù)據(jù)是否有交叉,全部顯示要配對(duì)的數(shù)據(jù)。
相關(guān)文章
SQL Server 2008 Express 及 Management Studio Express下載安裝配置教程
這篇文章主要講如何一步步從下載、安裝、配置 SQL Server 2008 Express 和 SMSS 到最后 使用 SMSS 連接本地的數(shù)據(jù)庫服務(wù),需要的朋友可以參考下2020-08-08SQL Server 2008 備份數(shù)據(jù)庫、還原數(shù)據(jù)庫的方法
這篇文章主要介紹了SQL Server 2008 備份數(shù)據(jù)庫、還原數(shù)據(jù)庫的方法,需要的朋友可以參考下2014-08-08Java打印和打印預(yù)覽機(jī)制實(shí)例代碼
這篇文章主要介紹了Java打印和打印預(yù)覽機(jī)制實(shí)例代碼,有需要的朋友可以參考一下2014-01-01如何在SQL Server 2008下輕松調(diào)試T-SQL語句和存儲(chǔ)過程
sqlserver2008調(diào)試的要求和條件:如果在引擎所在的電腦或服務(wù)器上調(diào)試,則只需要SA或者WINDOWS用戶登陸即可。如果是異地調(diào)試,則需要設(shè)置防火墻例外,增加SSMS和SQLSERVER.EXE為允許,增加135端口允許通過2013-10-10Excel導(dǎo)入數(shù)據(jù)庫時(shí)出現(xiàn)的文本截?cái)鄦栴}解決方案
在把Excel導(dǎo)入到數(shù)據(jù)庫中時(shí),發(fā)生文本截?cái)鄦栴}:即導(dǎo)入的數(shù)據(jù)每行只有一部分,遇到這樣的問題,甚是尷尬,接下來介紹此問題的解決方法,感興趣的朋友可以了解下,希望本文對(duì)你有所幫助2013-01-01