--表连接Join
--使用子查询
select StudentNo,StudentName,(select classname from Classes where ClassId=Student.ClassId) from Student
--使用from多表的方式
select Student.StudentNo,Student.StudentName,Classes.classname
from Student,Classes
where student.ClassId=classes.ClassId
order by Student.StudentNo
--表 inner/left/right join 表 on 如何建立关联
select Student.StudentNo,Student.StudentName,Classes.classname
from Student
--inner join:相当于from ..where,可以查询两个表建立关联字段值相等的记录 默认就是inner join
inner join Classes on Student.ClassId=Classes.ClassId
--查询学员的学号,姓名,班级名称,和考试成绩,科目名称
--建议:先确定你需要的字段来自于那一些表。。再来考虑这些表中如何建立关联
select Student.StudentNo,grade .classname,Student.StudentName,Result.StudentResult,Subject.SubjectName
from Student
--能够建立连接的表,能够做多表连接查询的表之间往往也可以建立主外键关系
inner join grade on Student.ClassId=grade .ClassId
inner join Result on Student.StudentNo=Result.StudentNo
inner join Subject on Result.SubjectId=Subject.SubjectId
--案例1:查询所有学生的姓名、年龄及所在班级
select Student.StudentName,DATEDIFF(yyyy,student.borndate,getdate()) age,Classes.classname
from Student
inner join Classes on Student.ClassId=Classes.ClassId
--案例2:查询年龄超过20岁的学生的姓名、年龄及所在班级
select Student.StudentName,DATEDIFF(yyyy,student.borndate,getdate()) age,Classes.classname
from Student
inner join Classes on Student.ClassId=Classes.ClassId
where DATEDIFF(yyyy,student.borndate,getdate())>20
--案例3:查询学生姓名、年龄、班级及成绩
select Student.StudentName,DATEDIFF(yyyy,student.borndate,getdate()) age,Classes.classname,Result.StudentResult
from Student
inner join Classes on Student.ClassId=Classes.ClassId
inner join Result on Student.StudentNo=Result.StudentNo
--left join:左连接
select * from PhoneNum
inner join PhoneType on PhoneNum.pTypeId=PhoneType.ptId
--使用左连接 左表:写在关键字前面的表就是左表 关键字后面的表就是右表 会得到左表的所有记录,如果建立关联的字段值在右表中没有存在,那么右表中的字段值就会以null值替代
select * from PhoneNum
left join PhoneType on PhoneNum.pTypeId=PhoneType.ptId
--右连接:会得到右表的所有记录,如果建立关联的字段值在左表中没有存在,那么左表中的字段值就会以null值替代
select * from PhoneNum
right join PhoneType on PhoneNum.pTypeId=PhoneType.ptId
--查询所有学生(参加及未参加考试的都算)及成绩
select * from Student
left join Result on Student.StudentNo=Result.StudentNo order by Student.StudentNo
--请查询出所有没有参加考试(在成绩表中不存在的学生)的学生的姓名。
select * from Student
left join Result on Student.StudentNo=Result.StudentNo
--判断右表的非空字段为null
where result.studentresult is null and Result.Id is null
order by Student.StudentNo
--练习1:查询所有英语及格的学生姓名、年龄及成绩
--1.使用表连接
select Student.StudentName,DATEDIFF(YYYY,Student.BornDate,GETDATE()),Result.StudentResult
from Student
inner join Result on Student.StudentNo=Result.StudentNo
inner join Subject on Result.SubjectId=Subject.SubjectId
where Result.StudentResult>=60 and Subject.SubjectName=‘office‘
--2.使用子查询
select Student.StudentName,DATEDIFF(YYYY,Student.BornDate,GETDATE()),Result.StudentResult
from Student
inner join Result on Student.StudentNo=Result.StudentNo
where StudentResult>=60 and SubjectId=(select SubjectId from Subject where SubjectName=‘office‘)
--练习2:查询所有参加考试的(english分数不为null)学生姓名、年龄及成绩
select Student.StudentName,DATEDIFF(YYYY,Student.BornDate,GETDATE()),Result.StudentResult
from Student
inner join Result on Student.StudentNo=Result.StudentNo
inner join Subject on Result.SubjectId=Subject.SubjectId
where Result.StudentResult is not null and Subject.SubjectName=‘office‘
--练习3:查询所有学生(参加和未参加考试)的学生姓名、年龄、成绩,如果没有参加考试显示缺考,如果小于60分显示不及格
select Student.StudentName,DATEDIFF(YYYY,Student.BornDate,GETDATE()),
case
when Result.StudentResult <60 then ‘不及格‘
when Result.StudentResult is null then ‘缺考‘
else cast(Result.StudentResult as CHAR(3)) --convert(目标类型,源数据)
end
from Student
left join Result on Student.StudentNo=Result.StudentNo
left join Subject on Result.SubjectId=Subject.SubjectId
select*from student
right join grade on grade .ClassId =Student .ClassId
原文:http://www.cnblogs.com/dianshen520/p/4352014.html