濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > sql server 2008中的apply運(yùn)算符使用方法

sql server 2008中的apply運(yùn)算符使用方法

熱門標(biāo)簽:海南自動(dòng)外呼系統(tǒng)價(jià)格 創(chuàng)業(yè)電銷機(jī)器人 九鹿林外呼系統(tǒng)怎么收費(fèi) 浙江地圖標(biāo)注 滄州營(yíng)銷外呼系統(tǒng)軟件 松原導(dǎo)航地圖標(biāo)注 沈陽(yáng)智能外呼系統(tǒng)代理 電銷機(jī)器人虛擬號(hào)碼 舞鋼市地圖標(biāo)注app

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ǔ)言如下:

復(fù)制代碼 代碼如下:

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ù),腳本如下

復(fù)制代碼 代碼如下:

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表中再插入一條記錄
復(fù)制代碼 代碼如下:

insert into StudentList(Name, Sex, Birthday, Class, Grade, regdate)
values('李銘', 1, '1989-05-04', 2, 7, '2010-05-05');

輸入以下語(yǔ)句
復(fù)制代碼 代碼如下:

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é)果展示出來。Cross Apply查詢確保在查詢兩個(gè)子集數(shù)據(jù)的交集時(shí),只有有效信息的集合才被列出來。

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

標(biāo)簽:商洛 公主嶺 寶雞 臺(tái)灣 日喀則 咸寧 ???/a> 西藏

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《sql server 2008中的apply運(yùn)算符使用方法》,本文關(guān)鍵詞  sql,server,2008,中的,apply,運(yùn)算符,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《sql server 2008中的apply運(yùn)算符使用方法》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于sql server 2008中的apply運(yùn)算符使用方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    浙江省| 天等县| 绵阳市| 呼伦贝尔市| 聊城市| 建瓯市| 胶州市| 刚察县| 禄丰县| 永福县| 饶阳县| 玉树县| 郯城县| 旬邑县| 正安县| 和平县| 漯河市| 陇川县| 武强县| 新干县| 贵南县| 绥中县| 肃宁县| 确山县| 濉溪县| 贵南县| 信阳市| 盐津县| 平乐县| 龙山县| 阿鲁科尔沁旗| 日照市| 寻甸| 镇平县| 浮山县| 田阳县| 锡林浩特市| 天气| 靖边县| 福泉市| 哈巴河县|