0

有人可以帮我获得所需结果的正确语法吗?在下面的查询中,子查询获取两个课程(A 和 B)。整个查询获取它们被持有的时间段。但是,我只想要两个班级共有的时期。

select m.period
  from course
  join meets m
 using (start_yy, school, class_cd)
  join section s
 using (start_yy, school, class_cd, section)
 where start_yy = '11'
   and school = 'MYSCH'
   and class_cd in (select cl.class_cd
                      from crslink cl, linkhead lh
                     where cl.start_yy = '11'
                       and cl.school = 'MYSCH'
                       and cl.seq_number = lh.seq_number)   
 order by period;

子查询中的两个类包含多个周期,但周期 4 是它们共同的唯一周期。我如何得到这个结果?

结果是:1,2,4,7

我需要:4

以下是包含查询中使用的字段的表结构。

TABLE_NAME,COLUMN_ID,COLUMN_NAME,DATA_TYPE,DATA_LENGTH
COURSE    ,1        ,START_YY   ,VARCHAR2 ,2
COURSE    ,2        ,SCHOOL     ,VARCHAR2 ,4
COURSE    ,3        ,CLASS_CD   ,VARCHAR2 ,10
CRSLINK   ,1        ,START_YY   ,VARCHAR2 ,2
CRSLINK   ,2        ,SCHOOL     ,VARCHAR2 ,4
CRSLINK   ,3        ,CLASS_CD   ,VARCHAR2 ,10
CRSLINK   ,4        ,SEQ_NUMBER ,NUMBER   ,22
LINKHEAD  ,1        ,START_YY   ,VARCHAR2 ,2
LINKHEAD  ,2        ,SCHOOL     ,VARCHAR2 ,4
LINKHEAD  ,7        ,SEQ_NUMBER ,NUMBER   ,22
MEETS     ,1        ,START_YY   ,VARCHAR2 ,2
MEETS     ,2        ,SCHOOL     ,VARCHAR2 ,4
MEETS     ,3        ,CLASS_CD   ,VARCHAR2 ,10
MEETS     ,4        ,SECTION    ,VARCHAR2 ,3
MEETS     ,6        ,PERIOD     ,VARCHAR2 ,2
SECTION   ,1        ,START_YY   ,VARCHAR2 ,2
SECTION   ,2        ,SCHOOL     ,VARCHAR2 ,4
SECTION   ,3        ,CLASS_CD   ,VARCHAR2 ,10
SECTION   ,4        ,SECTION    ,VARCHAR2 ,3
4

1 回答 1

0
select distinct m.period
  from course
  join meets m
 using (start_yy, school, class_cd)
  join section s
 using (start_yy, school, class_cd, section)
 where start_yy = '11'
   and school = 'MYSCH'
   and class_cd exists (select 1 
                      from crslink cl, linkhead lh
                     where cl.class_cd =course.class_cd 
                         And cl.start_yy = '11'
                       and cl.school = 'MYSCH'
                       and cl.seq_number = lh.seq_number)   
 order by period;

解决方案:

在以下查询中的 where 子句中为表名添加三个字段的前缀。

select m.period
  from course
  join meets m
 using (start_yy, school, class_cd)
  join section s
 using (start_yy, school, class_cd, section)
 join 
 crslink cl on cl.class_cd = class_cd
 join 
 linkhead lh on cl.seq_number = lh.seq_number
 where start_yy = '11'
   and school = 'MYSCH'
   and class_cd 
   and cl.start_yy = '11'
   and cl.school = 'MYSCH'
   and cl.class_cd is not null
   and lh.seq_number is not null
 order by period;
于 2012-05-03T14:09:16.257 回答