sql server 2008中的apply運(yùn)算符使用方法_Mssql數(shù)據(jù)庫(kù)教程
推薦:sql中case語(yǔ)句的用法淺談今天看資料的時(shí)候,看到了sql中使用case的一些使用方法,做個(gè)記錄
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ù)。
分享:SQL Server 數(shù)據(jù)庫(kù)基本操作語(yǔ)句總結(jié)SQL Server 數(shù)據(jù)庫(kù)基本操作語(yǔ)句總結(jié),需要的朋友可以參考一下
- sql 語(yǔ)句練習(xí)與答案
- 深入C++ string.find()函數(shù)的用法總結(jié)
- SQL Server中刪除重復(fù)數(shù)據(jù)的幾個(gè)方法
- sql刪除重復(fù)數(shù)據(jù)的詳細(xì)方法
- SQL SERVER 2000安裝教程圖文詳解
- 使用sql server management studio 2008 無(wú)法查看數(shù)據(jù)庫(kù),提示 無(wú)法為該請(qǐng)求檢索數(shù)據(jù) 錯(cuò)誤916解決方法
- SQLServer日志清空語(yǔ)句(sql2000,sql2005,sql2008)
- Sql Server 2008完全卸載方法(其他版本類似)
- sql server 2008 不允許保存更改,您所做的更改要求刪除并重新創(chuàng)建以下表
- SQL Server 2008 清空刪除日志文件(瞬間日志變幾M)
- Win7系統(tǒng)安裝MySQL5.5.21圖解教程
- 將DataTable作為存儲(chǔ)過(guò)程參數(shù)的用法實(shí)例詳解
Mssql數(shù)據(jù)庫(kù)教程Rss訂閱編程教程搜索
Mssql數(shù)據(jù)庫(kù)教程推薦
- 詳解MySQL權(quán)限
- 在SQL Server數(shù)據(jù)庫(kù)中批量導(dǎo)入數(shù)據(jù)的四種方法
- 一個(gè)獲取SQL Server數(shù)據(jù)字典的經(jīng)典SQL語(yǔ)句
- SQL Server記錄輪班的技巧
- SQL Server 索引基礎(chǔ)知識(shí)(4)----主鍵與聚集索引
- 談SQL Server數(shù)據(jù)庫(kù)管理常用的SQL和T-SQL語(yǔ)句
- 讓SQL Server數(shù)據(jù)庫(kù)自動(dòng)執(zhí)行管理任務(wù)(二)
- 詳解MSSQL的安全設(shè)置問(wèn)題
- 解析SQL Server數(shù)據(jù)應(yīng)用在不同的數(shù)據(jù)庫(kù)中
- 教你快速掌握數(shù)據(jù)庫(kù)設(shè)計(jì)范式的基本概念
猜你也喜歡看這些
- 淺談SQLite時(shí)間函數(shù)的使用說(shuō)明與總結(jié)分析
- MYSQL索引建立需要注意以下幾點(diǎn)細(xì)節(jié)
- MySQL筆記之基本查詢的應(yīng)用詳解
- MySQL筆記之函數(shù)查詢的使用
- MySQL的安全問(wèn)題從安裝開(kāi)始說(shuō)起
- 基于一致性hash算法(consistent hashing)的使用詳解
- mysql 定時(shí)更新表字段列的值狀態(tài)
- SQLServer導(dǎo)出數(shù)據(jù)到MySQL實(shí)例介紹
- 網(wǎng)站模板:以數(shù)據(jù)庫(kù)字段分組顯示數(shù)據(jù)的sql語(yǔ)句
- MySQL筆記之連接查詢?cè)斀?/a>
- 相關(guān)鏈接:
- 教程說(shuō)明:
Mssql數(shù)據(jù)庫(kù)教程-sql server 2008中的apply運(yùn)算符使用方法
。