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

所属分类: 数据库 / mssql2008 阅读数: 164
收藏 0 赞 0 分享

Apply运算符可以实现两个查询结果的全组合结果,又称为交叉集合。例如两个数据组合(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的外键

插入数据,脚本如下

复制代码 代码如下:

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');


两个表结构建立完毕,数据也成功插入进去了。为了便于讲解在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的区别

Cross Apply把语句两边的两个Select查询结果进行交叉配对,将所有结果展示出来。Cross Apply查询确保在查询两个子集数据的交集时,只有有效信息的集合才被列出来。

OuterApply查询是把两个子集的所有组合列了出来,不管数据是否有交叉,全部显示要配对的数据。

更多精彩内容其他人还在看

SQL Server 2008 数据库中创建只读用户的方法

这篇文章主要介绍了SQL Server 2008 数据库中创建只读用户的方法,为了保护数据库的安全,需要给不同的使用者开通不同的访问用户,那么如何简单的控制用户的权限呢?下面我们就创建一个只读用户,给大家学习使用
收藏 0 赞 0 分享

sql server 2008 用户 NT AUTHORITY\IUSR 登录失败的解决方法

今天在配置asp+sql server 2008的时候,发现总是无法登录数据库,提示sql server 2008 用户 NT AUTHORITY\IUSR 登录失败,原来是因为用户问题,这里简单分享下
收藏 0 赞 0 分享

当 MUST_CHANGE 为 ON (开)时,不能将 CHECK_POLICY 和 CHECK_EXPIRATION 选项设为 OFF (关)

这篇文章主要介绍了当 MUST_CHANGE 为 ON (开)时,不能将 CHECK_POLICY 和 CHECK_EXPIRATION 选项设为 OFF (关) ,需要的朋友可以参考下
收藏 0 赞 0 分享

Microsoft SQL Server 2008 基本安装说明

这篇文章主要介绍了Microsoft SQL Server 2008 基本安装说明 ,需要的朋友可以参考下
收藏 0 赞 0 分享

详解SQLServer 2008 R2数据库SSAS建模及扩展能力

本文主要介绍了SQLServer 2008 R2数据库SSAS建模工具和建模的关键过程以及数据挖掘方面的知识,需要的朋友可以参考下
收藏 0 赞 0 分享

sql2008安装教程 SQL Server 2008 R2 安装图解

这篇文章主要介绍了sql2008安装教程 SQL Server 2008 R2 安装图解,需要的朋友可以参考下
收藏 0 赞 0 分享

SQL Server 2008 密钥 key 使用方法

最新修订版和Visual Studio 2008一样,从官网下载SQL Server 2008的180天试用版其实与正式版内容是基本相同的,唯一的区别就在于安装配置文件中所包含的key。各种版本的SQL Server在进行到这一步之前都是完全一样的
收藏 0 赞 0 分享

sql server 2008 r2 express 精简版与企业版的区别

今天群里发现有网站问sql server 2008 r2 express是什么版本,其实express表示此版本是精简版的,方便学习使用,主要是免费的
收藏 0 赞 0 分享

Win2003系统下SQL Server 2008安装图解教程(详细图解)

这篇文章主要介绍了Win2003系统下SQL Server 2008安装图解教程(详细图解),需要的朋友可以参考下
收藏 0 赞 0 分享

SQL Server 2008 R2 超详细安装图文教程

这篇文章主要介绍了SQL Server 2008 R2 超详细安装图文教程,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多