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理解的技巧?还是说,看多了,有自己的一套理解?

解决方案 »

  1.   

    感觉看一下理论方面的书就可以了.
    我感觉SQL 查询主要就是看它的笛卡尔积.
    如果小的话速度就快.如果大的话速度就慢了.
      

  2.   

    第一步:(select s#,score from SC where C#='001') a,这是一个新的表,,(select s#,score from SC where C#='002') b 这也是一个新的表。 
    第二步:关联上述两个表,比较数据得到结果.
      

  3.   

    1.执行select s#,score from SC where C#='001',生成临时表A
    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# 我的理解应该是这样的
      

  4.   

    (select s#,score from SC where C#='001') 其实是一个子查询,你可以把它看成一个临时表
    (select s#,score from SC where C#='002') 也是如此,
    其实在SQL执行的时候这两个子查询都是在内存中,并不是物理数据,
    查询完后,自动消失。可以按你的第一种理解
      

  5.   

    你也可以:SQL--"查询"--“显示估计的执行计划”看到具体的执行过程
      

  6.   

    也可以直接这样:select a.S#
    from sc a , sc b
    where a.C#='001' and b.C#='002' and a.score > b.score and a.s#=b.s#