sql server 2008中的apply运算符使用方法
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 Server2008R2,第一次使用时在修改表结构的时候经碰到这样一个警告【不允许保存更改。您所做的更改要求删除并重新创建以下表.对这样的错误提示怎么解决呢?下面小编通过图文并茂的形式给大家分享解决办法2017-01-01
SQL Server 2008用''sa''登录失败,启用''sa''登录的解决办法
这篇文章主要介绍了SQL Server 2008用'sa'登录失败,启用'sa'登录的解决办法,感兴趣的小伙伴们可以参考一下2015-09-09
解决SQL SERVER 2008数据库表中修改字段后不能保存
SQL SERVER 2008数据库表中修改字段后不能保存,这种情况将阻止保存要求重新创建表的更改一项的钩钩去掉就OK了2013-11-11
SQL2008中SQL应用之- 死锁(Deadlocking)
当一个用户会话(会话1)已经落定了一个资源,而另一个会话(会话2)想要修改该资源,并且会话2也锁定了会话1想要修改的资源时,就会出现“死锁”(deadlocking)。2011-06-06
windows系统下SQL Server 2008超详细安装教程
有很多同学不知道怎么安装SQL Server 2008,今天我就在这里教大家怎么安装SQL Server 2008,下面这篇文章主要给大家介绍了关于windows系统下SQL Server 2008超详细安装教程的相关资料,需要的朋友可以参考下2022-12-12
SQLServer 2008中SQL增强之三 Merge(在一条语句中使用Insert,Update,Delete)
SQLServer 2008中SQL增强之三 Merge(在一条语句中使用Insert,Update,Delete)2011-05-05
SQLSERVER2008中CTE的Split与CLR的性能比较
之前曾有一篇POST是关于用CTE实现Split,这种方法已经比传统的方法高效了。今天我们就这个方法与CLR实现的Split做比较。在CLR实现Split函数的确很简单,dotnet framework本身就有这个function了。2011-10-10


最新评论