首页数据库sql server 2008中的apply运算符使用方法

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

时间2024-02-28 19:31:03发布访客分类数据库浏览596
导读:收集整理的这篇文章主要介绍了sql server 2008中的apply运算符使用方法,觉得挺不错的,现在分享给大家,也给大家做个参考。 Apply运算符可以实现两个查询结果的全组合结果...
收集整理的这篇文章主要介绍了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查询是把两个子集的所有组合列了出来,不管数据是否有交叉,全部显示要配对的数据。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: sql server 2008中的apply运算符使用方法
本文地址: https://pptw.com/jishu/631848.html
SQLServer 2008中的代码安全(一) 存储过程加密与安全上下文 SQL Server 2008中的代码安全(二) DDL触发器与登录触发器

游客 回复需填写必要信息