最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

sql server 2008中的apply運算符使用方法

 更新時間:2013年05月17日 17:44:46   作者:  
sql server 2008中的apply運算符使用方法,需要的朋友可以參考一下

Apply運算符可以實現(xiàn)兩個查詢結果的全組合結果,又稱為交叉集合。例如兩個數(shù)據(jù)組合(A,B)、(A,B),他們的交叉集合為(AA,AB,AA,AB)。

Apply分為Cross Apply和Outer Apply兩種使用方式。具體分析如下:

首先先建立兩個表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');


兩個表結構建立完畢,數(shù)據(jù)也成功插入進去了。為了便于講解在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;

結果如下

再輸入以下語句

select * from StudentList a
outer apply
(select ClassID, Score from ScoreInfo where StudentID=a.id) b;

結果如下

可以看出Cross Apply和Outer Apply的區(qū)別

Cross Apply把語句兩邊的兩個Select查詢結果進行交叉配對,將所有結果展示出來。Cross Apply查詢確保在查詢兩個子集數(shù)據(jù)的交集時,只有有效信息的集合才被列出來。

OuterApply查詢是把兩個子集的所有組合列了出來,不管數(shù)據(jù)是否有交叉,全部顯示要配對的數(shù)據(jù)。

相關文章

最新評論

梅河口市| 宜阳县| 德州市| 高碑店市| 莲花县| 额济纳旗| 隆林| 石屏县| 宁安市| 石景山区| 满洲里市| 磐石市| 瓮安县| 广灵县| 新邵县| 阜康市| 金乡县| 汉阴县| 锡林郭勒盟| 浑源县| 广昌县| 兴安县| 景东| 霍邱县| 邹城市| 郁南县| 米易县| 临沧市| 金堂县| 三门峡市| 晴隆县| 神池县| 沿河| 抚顺县| 柯坪县| 中宁县| 平罗县| 什邡市| 阳东县| 新安县| 徐州市|