Student(S#,Sname,Sage,Ssex) 学生表
Course(C#,Cname,T#) 课程表
SC(S#,C#,score) 成绩表
Teacher(T#,Tname) 教师表 问题:
1、查询“001”课程比“002”课程成绩高的所有学生的学号;
select a.S# from
(select s#,score from SC where C#='001') a,(select s#,score from SC where C#='002') b
where a.score>b.score and a.s#=b.s#;
=====================================
这样理解好不好:
第一步:(select s#,score from SC where C#='001') a,这是一个新的表,,(select s#,score from SC where C#='002') b 这也是一个新的表。
第二步:a, b where a.score>b.score and a.s#=b.s#; 这样又组成一个新的表,暂且称为c
第三步:select a.S# from c。从新表c 里取数据。还是这种理解:
先select a.S# from
(select s#,score from SC where C#='001') a,(select s#,score from SC where C#='002') b
再筛选where a.score>b.score and a.s#=b.s#;
有没有关于sql理解的技巧?还是说,看多了,有自己的一套理解?
Course(C#,Cname,T#) 课程表
SC(S#,C#,score) 成绩表
Teacher(T#,Tname) 教师表 问题:
1、查询“001”课程比“002”课程成绩高的所有学生的学号;
select a.S# from
(select s#,score from SC where C#='001') a,(select s#,score from SC where C#='002') b
where a.score>b.score and a.s#=b.s#;
=====================================
这样理解好不好:
第一步:(select s#,score from SC where C#='001') a,这是一个新的表,,(select s#,score from SC where C#='002') b 这也是一个新的表。
第二步:a, b where a.score>b.score and a.s#=b.s#; 这样又组成一个新的表,暂且称为c
第三步:select a.S# from c。从新表c 里取数据。还是这种理解:
先select a.S# from
(select s#,score from SC where C#='001') a,(select s#,score from SC where C#='002') b
再筛选where a.score>b.score and a.s#=b.s#;
有没有关于sql理解的技巧?还是说,看多了,有自己的一套理解?
我感觉SQL 查询主要就是看它的笛卡尔积.
如果小的话速度就快.如果大的话速度就慢了.
第二步:关联上述两个表,比较数据得到结果.
2.select s#,score from SC where C#='002',生成临时表B
3.根据a.score>b.score and a.s#=b.s#对A,B进行JOIN连接
4.select a.S# 我的理解应该是这样的
(select s#,score from SC where C#='002') 也是如此,
其实在SQL执行的时候这两个子查询都是在内存中,并不是物理数据,
查询完后,自动消失。可以按你的第一种理解
from sc a , sc b
where a.C#='001' and b.C#='002' and a.score > b.score and a.s#=b.s#