數據表
1).學生表: Student
字段: (SID,Sname,Sage,Ssex) -SID學生編號,Sneme學生姓名,Sage出生年月,Ssex學生性別
2).課程表: Course
字段: (CD,Cname,TI) –CID課程編號,Cname課程名稱,TID教師編號
3).教師表: Teacher
字段: (TID,Tname) -ID教師編號,Tname教師姓名
4).成績表: SC
字段: (SID,CID,score) -SID學生編號,CID課程編號,score分數
select查詢
1.單表查詢
問題:查詢SID為1的學生信息
查詢語句:select Sname,Sage,Ssex from student where SID=1
2.多表查詢
問題:查詢"01"課程比”02”課程成績高的學生的信息及課程分數(這里01和02課程指的是CID)
方法一:from多表查詢
1)第一步先查出所有學生的01課程的分數,查詢結果作為a表
查詢語句:select SID,CID,score from sc where CID=01
2)第二步再查出所有學生的02課程的分數,查詢結果作為b表
查詢語句:select SID,CID,score from sc where CID=02
3)第三步查詢同一個學生的01課程分數大於02課程分數的學生SID以及課程分數,查詢結果作為c表
查詢語句:select a.SID,a.score as score01,b.score as score02 from (select SID,CID,score from sc where CID=01) as a,(select SID,CID,score from sc where CID=02) as b
where a.SID = b.SID and a.score>b.score
4)第四步根據c表和student表查詢出學生具體信息
查詢語句:select d.SID,d.Sname,d.Sage,d.Ssex,score01,score02 from student as d,
(
select a.SID,a.score as score01,b.score as score02 from (select SID,CID,score from sc where CID=01) as a,(select SID,CID,score from sc where CID=02) as b
where a.SID = b.SID and a.score>b.score
) as c
where d.SID=c.SID
方法二:使用join連接
1)第一步先查出所有學生的01課程的分數,查詢結果作為a表
查詢語句:select SID,CID,score from sc where CID=01
2)第二步再查出所有學生的02課程的分數,查詢結果作為b表
查詢語句:select SID,CID,score from sc where CID=02
3)第三步使用join連接student表、a表和b表
查詢語句:select c.SID, c.Sname,c.Sage, c.Ssex,a.score as score01, b.score as score02 from student as c
right join (select SID,CID,score from sc where CID=01) as a on a.SID=c.SID
right join (select SID,CID,score from sc where CID=02) as b on b.SID=c.SID
where a.SID=b.SID and a.score>b.score