目次
学生テーブル
scテーブル
そしてクエリ
クエリとは何ですか?
2つのselectステートメントの結果を組み合わせる
図に示すように、クエリされたA結果セットとB結果セットを組み合わせます。
コード:
select a.CId,a.score ,b.Sname --找出指定学生
from sc a, student b
where a.SId =b.SId and CId = 01
union
select ' ',sum(score),'合计' --成绩总数
from sc a, student b
where a.SId =b.SId and CId = 01
結果:
問い合わせ
お問い合わせとは?
2つのselectステートメントの結果セットを組み合わせて、それらが共通している部分を見つける
図に示すように、結果セットAと結果セットBを取得し、それらが一緒に持つ結果セットCを要求します。
コード:
select a.CId ,a.score ,b.Sname from sc a ,student b --查询课程为801,考试为01的学生
where a.SId =b.SId and a.CId ='01' and a.课程编号 ='801'
intersect
select top 10 a.CId ,a.score ,b.Sname from sc a, student b --查询课程为801,成绩前10的学生
where a.SId =b.SId and a.课程编号 ='801' order by a.score desc
結果:
不正なクエリ
差分クエリはどの部分を探しますか?
2つの結果セットが交差したくない場所を削除します
コード:
select a.CId ,a.score ,b.Sname from sc a ,student b --查询考试为01的学生
where a.SId =b.SId and a.CId ='01'
except
select top 15 a.CId ,a.score ,b.Sname from sc a, student b --查询课程为801,成绩前10的学生
where a.SId =b.SId and a.课程编号 ='801' order by a.score desc
結果: