查询中的结果
?
?
--子查询的三种使用方式
--1.子查询做为条件,子查询接在关系运算符后面 ?> ?< >= <= = <> !=,如果是接这关系运算符后面,必须保证 子查询只返回一个值
--查询六期班的学员信息
select * from Student where ClassId=(select ClassId from grade where classname='八期班')
--子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
select * from Student where ClassId=(select ClassId from grade)
--查询八期班以外的学员信息
--当子查询返回多个值(多行一列),可以使用in来指定这个范围
select * from Student where ClassId in(select ClassId from grade where classname<>'八期班')
--当没有用 EXISTS 引入子查询时,在选择列表中只能指定一个表达式。如果是多行多列或者一行多列就需要使用exists
--使用 EXISTS 关键字引入子查询后,子查询的作用就相当于进行存在测试。外部查询的 WHERE 子句测试子查询返回的行是否存在
select * from Student where ?EXISTS(select * from grade)
select * from Student where ?ClassId in(select * from grade)
?
--2.子查询做为结果集--
select top 5 * from Student --前五条
--使用top分页
select top 5 * from Student where StudentNo not in(select top 5 studentno from Student)
--使用函数分页 ?ROW_NUMBER() over(order by studentno),可以生成行号,排序的原因是因为不同的排序方式获取的记录顺序不一样
select ROW_NUMBER() over(order by studentno),* from Student
--查询拥有新生成行号的结果集 ?注意:1.子查询必须的别名 ?2.必须为子查询中所有字段命名,也就意味着需要为新生成的行号列命名
select * from (select ROW_NUMBER() over(order by studentno) id,* from Student) temp where temp.id>0 and temp.id<=5
select * from (select ROW_NUMBER() over(order by studentno) id,* from Student) temp where temp.id>5 and temp.id<=10
select * from (select ROW_NUMBER() over(order by studentno) id,* from Student) temp where temp.id>10 and temp.id<=15
?
--3.子查询还可以做为列的值
select (select studentname from student where studentno=result.studentno),(select subjectname from subject where subjectid=result.SubjectId), StudentResult from Result
?
--使用Row_number over()实现分页
--1.先写出有行号的结果集
select ROW_NUMBER() over(order by studentno),* from Student
--2.查询有行号的结果集 子查询做为结果集必须添加别名,子查询的列必须都有名称
select * from (select ROW_NUMBER() over(order by studentno) id,* from Student) temp where id>0 and id<=5
--查询年龄比“廖杨”大的学员,显示这些学员的信息
select * from Student where BornDate<(select BornDate from Student where StudentName='廖杨')
--查询二期班开设的课程
select * from Subject where ClassId=(select ClassId from grade where classname='二期班')
--查询参加最近一次“office”考试成绩最高分和最低分
--1查询出科目 ID
select subjectid from Subject where SubjectName='office'
--2.查询出这一科目的考试日期
select MAX(ExamDate) from Result where SubjectId=(select subjectid from Subject where SubjectName='office')
--3,写出查询的框架
select MAX(StudentResult),MIN(StudentResult) from Result where SubjectId=() and ExamDate=()
--4.使用子查询做为条件
select MAX(StudentResult),MIN(StudentResult) from Result where SubjectId=(
? ? select subjectid from Subject where SubjectName='office'
? ? ? ? ) and ExamDate=(
? ? ? ? ? ? ? ? select MAX(ExamDate) from Result where SubjectId=(
? ? ? ? ? ? ? ? ? ? ? ? select subjectid from Subject where SubjectName='office'
? ? ? ? ? ? ? ? ? ? ? ? )
? ? ? ? ? ? ? ? ? ? )
?
?
?
16.表连接Join
?
--1.inner join :能够找到两个表中建立连接字段值相等的记录
--查询学员信息显示班级名称
select Student.StudentNo,Student.StudentName,grade.classname
from Student
inner join grade on Student.ClassId=grade.ClassId
--左连接: 关键字前面的表是左表,后面的表是右表
--左连接可以得到左表所有数据,如果建立关联的字段值在右表中不存在,那么右表的数据就以null值替换
select PhoneNum.*,PhoneType.*
from ? PhoneNum ?
left join ?PhoneType on PhoneNum.pTypeId=PhoneType.ptId
--右连接: 关键字前面的表是左表,后面的表是右表
--右连接可以得到右表所有数据,如果建立关联的字段值在右左表中不存在,那么左表的数据就以null值替换
select PhoneNum.*,PhoneType.*
fr