2011-05-06 86 views
0

下面是我的查詢基本上那裏有一個包含學生課程信息的studentsection表,這張表除了課程描述之外還有我需要的一切。這包含在課程表中。出於某種原因,我似乎無法只從studentsection表中獲取條目,我嘗試的每個查詢都會從課程表中獲取所有內容。任何幫助深表感謝!mysql加入不到期望的結果

select distinct studentid 
       , s.deptnum 
       , s.coursenum 
       , coursedesc 
       , sectionnum 
       , quarter 
       , grade 
      from studentsection c 
      left 
      join course c 
      on s.deptnum = c.deptnum 
      where exists(select deptnum from course) 
      order 
      by studentid 
       , quarter 
       , grade; 

這裏是我的表定義:

create table StudentSection(
    studentID int, 
    deptNum char(4), 
    courseNum char(3), 
    sectionNum char(2), 
    quarter char(3), 
    grade varchar(1), 
    constraint studentSection_PK primary key(studentID, deptNum, courseNum, sectionNum, quarter), 
    constraint studentSection_section_FK foreign key (deptNum, courseNum, sectionNum, quarter) references Section(deptNum, courseNum, sectionNum, quarter) 
) 

CREATE TABLE Course (
    deptNum char(4), 
    courseNum char(3), 
    courseDesc varchar(50) NOT NULL, 
    creditHrs integer, 
    CONSTRAINT course_PK PRIMARY KEY (deptNum, courseNum), 
    CONSTRAINT course_department_FK FOREIGN KEY (deptNum) REFERENCES department(deptNum)) 

CREATE TABLE Section (
    deptNum  char(4), 
    courseNum char(3), 
    sectionNum char(2), 
    quarter  char(3), 
    instructor varchar(20), 
    CONSTRAINT section_PK PRIMARY KEY (deptNum, courseNum, sectionNum, quarter), 
    CONSTRAINT section_course_FK FOREIGN KEY (deptNum, courseNum) REFERENCES course(deptNum, courseNum)); 
+0

您是否加入正確的領域? 'deptnum'是'course'的'PRIMARY KEY'嗎?你能否發表你的表格定義? – Quassnoi 2011-05-06 11:31:27

回答

0

我不知道如果我得到你的問題的權利,但你嘗試了inner join

+0

是的,我嘗試了所有不同的連接... – mespo365 2011-05-06 11:49:40