黑马程序员--高级查询

来源:互联网 发布:淘宝店铺如何发布宝贝教程 编辑:程序博客网 时间:2024/05/03 04:23
---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ---------------------->高级查询就是对已有数据进行复杂的统计、分析,去提供给领导进行决策,这就是“报表应用”。

>在一个项目中,报表应用通常由项目经理、技术总监亲自完成,因为报表的阅读者通常就是软件购买决策者

一、子查询

1、语法

>select ...... from 1 where1 > (子查询)

2、概述

>习惯上,外面的查询称为父查询,圆括号中嵌入的查询称为子查询。SQL Server执行时,先执行子查询部分,求出子查询部分的值,然后再执行整个父查询,返回最后的结果

>因为子查询作为where条件的一部分,所以还可以是updateinsertdelete一起使用,语法类似于select

3、实例

select StudentNo,StudentName,Sex,BornDate,Address, from Student

              where BornDate>

              (select BornDate from Student where StudentName='李斯文')

4、注意

>将子查询和比较运算符联合使用,必须保证子查询返回的值不能多于一个。如果子查询返回的值为null,则查出0条记录

>一般来说,表联接可以用子查询替换,但反过来却不一定,有的子查询不能用表连接来替换。优先使用表连接,因为所有的数据库引擎都针对jion语法进行过特别优化,例如:构建

视图

二、innot in子查询

1in子查询

1> in后面的子查询可以返回多条记录,用于限制筛选范围

2> 实例:

select StudentName from Student where StudentNo in

              (select StudentNo from Result where SubjectNo=

              (select SubjectNo from Subject where SubjectName='Java Logic'))

       select distinct Student.StudentNo,StudentName from Student

              inner join Result on Result.StudentNo=Student.StudentNo

              where ExamDate in

              (select max(ExamDate) from Result where SubjectId=

              (select SubjectId from Subject where SubjectName='Java Logic'))

3> 附加

>distinct是抑制重复

>select distinct StudentNo,StudentName from Student

2not in子查询

1> not inin相反

2> not in实现N条数据的分页

select top 每页行数 StudentNo,StudentName from Student

              where StudentNo not in

              (select top (页码-1)*每页行数 StudentNo from Student)

---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------