您现在的位置是:网站首页> 编程资料编程资料
sql server 2008中的apply运算符使用方法_mssql2008_
2023-05-27
613人已围观
简介 sql server 2008中的apply运算符使用方法_mssql2008_
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查询是把两个子集的所有组合列了出来,不管数据是否有交叉,全部显示要配对的数据。
相关内容
- AD域中成员服务器SQL 2008 Server安装配置图文教程_mssql2008_
- 深入SqlServer2008 数据库同步的两种方式(Sql JOB)的分析介绍_mssql2008_
- MyEclipse 配置SQL Server 2008数据库驱动操作步骤_mssql2008_
- 清理SQL Server 2008日志文件Cannot shrink log file 2 的解决方案_mssql2008_
- SQL SERVER 2008 中三种分页方法与比较_mssql2008_
- 如何把Excel数据导入到SQL2008数据库的实例方法_mssql2008_
- SQL2008 附加数据库提示 5120错误 解决办法_mssql2008_
- 使用Sqlserver事务发布实现数据同步(sql2008)_mssql2008_
- sql2008评估期已过的解决办法及序列号_mssql2008_
- sql2008安装后服务器改名造成名称不一致解决方法_mssql2008_
