--錶鏈接Join--sql
--查詢全部學生的姓名、年齡及所在班級c#
--使用子查詢ssh
select StudentName,DATEDIFF(yyyy,borndate,getdate()),(select classname from grade where ClassId=Student.ClassId) from Student函數
--使用from多表spa
select StudentName,DATEDIFF(yyyy,borndate,getdate()),classname排序
from Student,gradeget
where Student.ClassId=grade.ClassIdast
--使用多表鏈接 表 inner join 另外的表 on 如何創建關聯(可以創建關聯的字段通常就是可以建立主外鍵關係的字段)class
select StudentName,DATEDIFF(yyyy,borndate,getdate()),classname基礎
from Student
join grade on Student.ClassId=grade.ClassId
--1.內鏈接:inner join.找到兩個表創建關係的字段值相等的記錄,若是字段值不相等,那麼就拋棄
--查詢學生姓名、年齡、班級及成績
select Student.StudentName,DATEDIFF(YYYY,Student.BornDate,GETDATE()),grade.classname,Result.StudentResult
from Student
inner join grade on Student.ClassId=grade.ClassId
inner join Result on Student.StudentNo=Result.StudentNo
where student.ClassId=2
--查詢每一個學員須要參加的考試科目名稱
select Student.StudentNo,Student.StudentName,Subject.SubjectName
from Student
inner join Result on Student.StudentNo=Result.StudentNo
inner join Subject on Result.SubjectId=Subject.SubjectId
select Student.StudentNo,Student.StudentName,Subject.SubjectName
from Student
inner join Subject on Student.ClassId=Subject.ClassId
select * from PhoneType
inner join PhoneNum on PhoneNum.pTypeId=PhoneType.ptId
--左,右鏈接
--左鏈接,能夠獲得左表的全部記錄,只不過若是左表的字段的值在右表中找不到相應的關聯記錄,那麼右表中的全部字段會以null值替代
select * from PhoneType left join PhoneNum on PhoneNum.pTypeId=PhoneType.ptId
--查詢沒有參加考試的學員信息
select * from Student
left join Result on Student.StudentNo=Result.StudentNo
where Result.StudentNo is null and Result.SubjectId is null
--右鏈接:
select * from PhoneType left join PhoneNum on PhoneNum.pTypeId=PhoneType.ptId
--交叉鏈接:
select * from Student
cross join grade
--作多表鏈接的建議:
--1.先肯定字段都來自於那一些表,經過 表.字段 的方式進行肯定
--2.查看這些表都有那一些關聯(能夠創建主外鍵關係的字段),若是沒有關聯,考慮是否須要使用中間其它表進行關聯
--3.多表鏈接後能夠獲得一個虛擬的表,能夠對這個表的全部字段添加where條件
--練習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 Subject.SubjectName='office' and Result.StudentResult>=60
--練習2:查詢全部參加考試的(english分數不爲null)學生姓名、年齡及成績
--練習3:查詢全部學生(參加和未參加考試)的學生姓名、年齡、成績,若是沒有參加考試顯示缺考,若是小於60分顯示不及格office
select Student.StudentName,DATEDIFF(yyyy,student.borndate,getdate()),
case
when Result.StudentResult is null then '沒有考試'
when Result.StudentResult>=60 then cast(Result.StudentResult AS CHAR(3))
else '不及格'
end
from Student
left join Result on Student.StudentNo=Result.StudentNo
inner join Subject on Result.SubjectId=Subject.SubjectId
where Subject.SubjectName='office'
---變量的種類--全局變量
go
insert into grade values('fdasfas')
select * from Student
select @@IDENTITY ---它獲得的值不是指點上一條語句,而是最近的insert語句的值
go
select * from Student
select @@ROWCOUNT
delete from Student where StudentNo=12433
select @@ROWCOUNT
go
--@@ERROR 獲得最近這一條語句的錯誤號
select * from stu --語法錯誤的錯誤號得不到
select @@ERROR --針對於增長刪除和修改而言,若是有錯誤那麼錯誤號必定大於0,若是沒有錯誤那麼就必定是0
update Student set LoginPwd='ss' where StudentNo=1
update Student set LoginPwd='ss' where StudentNo=1
select * from Student
go
select @@ERROR
---視圖
---視圖的本質就是一句sql語句--select.它表示一張表的部分數據或多張表的綜合數據,其結構和數據是創建在對錶的查詢基礎上
--視圖的本質就是一張虛擬表:它的操做與操做表基本一致
--視圖裏面並無真正存儲數據,它存儲的是sql語句,當執行視圖獲取數據的時候,本質就是執行視圖中的sql語句去獲取數據
select * from Student
select * from vw_getstudentResultInfo
--經過命令語句建立視圖
--語法:
--go
--create view vw_自定義名稱
--as
-- select 命令
--go
--獲取六期班學員信息
--1.可不能夠寫多條select查詢?只能寫一條
--2。視圖裏面只能寫select,不能添加任何的update/delete/insert語句
if exists(select * from sysobjects where name='vw_getStudentByClassId_6')
drop view vw_getStudentByClassId_6
go
create view vw_getStudentByClassId_6
as
select top 2 * from Student order by studentname
go
--對視圖進行查詢
select * from vw_getStudentByClassId_6 order by studentname
--對視圖進行修改,能夠對視圖進行刪除增長和修改操做,何況操做會直接影響物理表,因此通常不創建去作這些操做,由於視圖的本質目的是爲查詢
update vw_getStudentByClassId_6 set classhour = 10, subjectname='c#', classname='111' where subjectid=2
delete from vw_getStudentByClassId_6 where Studentno=3
--視圖的增長刪除和修改操做只能針對於單個表,若是涉及了多個表的操做,那麼將不成功
select * from vw_getstudentResultInfo
delete from vw_getstudentResultInfo where Studentno=6
update vw_getstudentResultInfo set studentresult=55 , subjectname='c#' where Studentno=6
--CREATE PROC[EDURE]
--procedure
--子查詢--
--一個查詢裏面還包含着另一個查詢
--子查詢作爲條件--
--1.包含在()裏面先執行的查詢就是子查詢語句,包含子查詢的就稱爲父查詢語句
--2.引入子查詢時,在選擇列表中只能指定一個表達式,就是意味着子查詢作爲條件(有關係運算符)的時候子查詢永遠不可能出現多列的狀況
--3.子查詢返回的值不止一個。當子查詢跟隨在 =、!=、<、<=、>、>= 以後,或子查詢用做表達式時,這種狀況是不容許的,就意味着若是子查詢跟在關係運算符以後,必須保證子查詢只返回了單個值,若是真的須要這麼作就可使用關鍵字 in/not in
--查詢比林思年齡大的學員信息
--4.有子查詢的sql語句返回的列只與父查詢有關,子查詢中的列僅僅是作爲條件判斷
--1.先查詢出林思的年齡
select borndate from Student where StudentName='林思'
--返回一行多列值----作爲條件永遠錯誤
select * from Student where BornDate<(select * from Student where StudentName='林思')
--返回多行一列值
select * from Student where BornDate<(select borndate from Student)
--查詢班級ID《=3的學員信息
select classid from grade where ClassId<=3
select StudentNo,StudentName from Student where ClassId not in(select classid from grade where ClassId<=3)
--子查詢獲得某列的值 --用得最少
--查詢學員是10的學員office考試成績和考試日期,顯示學員姓名
select (select studentname from student where studentno=10),StudentResult,ExamDate from Result where StudentNo=10 and SubjectId=(select SubjectId from Subject where SubjectName='office')
--3.查詢獲得結果集,可使用子查詢返回一張虛擬表
--查詢班級ID爲1的全部學員的考試成績
--1查詢出班級ID爲1的學員的學號
select studentno from Student where ClassId=1
--查詢指定學號的學員的成績
select * from Result where StudentNo in(select studentno from Student where ClassId=1)
--分頁
select top 5 * from Student
select top 5 * from Student where StudentNo not in(select top 5 StudentNo from Student order by StudentNo)
--ROW_NUMBER 能夠爲查詢出的每一行返回一個行號,行號相似於標識列,永遠不會重複,同是它默認是連續的,這個函數能夠爲結果集添加一個新列
--over說明在那一個字段上進行排序,由於按不一樣字段排序結果集的顯示是不同的
select ROW_NUMBER() over(order by studentno) as id,* from Student
--若是子查詢作爲結果集那麼必須爲其添加別名
select * from (select ROW_NUMBER() over(order by studentno) as id,* from Student) as temp
where temp.id>=6 and id<=10
--使用Row_number() over(指定排序字段)實現分頁,這個函數能夠爲結果集添加一個新列
select ROW_NUMBER() over(order by studentno) as id,* from Student
select * from (select ROW_NUMBER() over(order by studentno) as id,* from Student
) as 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」考試成績最高分和最低分
select subjectid from Subject where SubjectName='office'
select MAX(ExamDate) from Result where SubjectId=(select subjectid from Subject where SubjectName='office')
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'))
select * from Result
select * from Subject
--子查詢的分類:
--1.獨立子查詢:單獨能夠運行的子查詢
--2.相關子查詢:子查詢中引用了父查詢中的列
--查詢參加了考試的學員信息
select distinct StudentNo from Result
select * from Student where StudentNo in(select distinct StudentNo from Result)
select * from Student where StudentNo=(select distinct StudentNo from Result where Result.StudentNo=Student.StudentNo)
select StudentNo from Result where StudentNo=33
select * from Student where StudentNo=(select StudentNo from Result where StudentNo=33)