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

所属分类: 数据库 / mssql2008 阅读数: 97
收藏 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查询是把两个子集的所有组合列了出来,不管数据是否有交叉,全部显示要配对的数据。

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

sql2008设置subcategory报表参数可用值和默认值步骤分享

sql2008设置subcategory报表参数可用值和默认值步骤分享,大家参考使用吧
收藏 0 赞 0 分享

sqlserver2008锁表语句详解(锁定数据库一个表)

锁一个SQL表的语句是SQL数据库使用者都需要知道的,下面就将为您介绍锁SQL表的语句,希望对您学习锁SQL表方面能有所帮助
收藏 0 赞 0 分享

sqlserver2008安装报语言不符的解决方法

安装sqlserver2008R2报语言不符解决方法,大家参考使用吧
收藏 0 赞 0 分享

sqlserver2008查看表记录或者修改存储过程出现目录名无效错误解决方法

登陆数据库后,右键打开表提示:目录名无效,执行SQL语句也提示有错误,现在把解决方法分享给大家
收藏 0 赞 0 分享

sqlserver2008自动发送邮件

sqlserver2008自动发送邮件
收藏 0 赞 0 分享

Java打印和打印预览机制实例代码

这篇文章主要介绍了Java打印和打印预览机制实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

sql server 2008数据库连接字符串大全

这篇文章主要介绍了sql server 2008数据库的连接字符串大全,需要的朋友可以参考下
收藏 0 赞 0 分享

SQL Server 2008 安装SQLDMO.dll的方法

ASP.NET利用它可以实现在线备份、还原数据库等各种功能
收藏 0 赞 0 分享

SQL Server把单个用户转换成多个用户的方法

这篇文章主要介绍了SQL Server把单个用户转换成多个用户的方法,一条语句即可解决,需要的朋友可以参考下
收藏 0 赞 0 分享

SQL SERVER 2008 R2 重建索引的方法

项目升级数据库由SQL2000升级到2008R2,今天对数据库表进行碎片扫描,发现有些表碎片较大,于是决定重建索引,MSDN联机帮助是最好的老师,将相关脚本摘录备后查
收藏 0 赞 0 分享
查看更多