我该怎么做这个选择语句

时间:2014-09-03 17:03:46

标签: mysql sql join except

我有两个sql语句

第一个

SELECT 
    course.CourseNum, 
    course.CourseName, 
    course.CreditHours
FROM course
WHERE course.TypeID=1

结果是

Course Num------Course Name---------Credit Hours                                       
1101----------Arabic Language----------3             
1103----------English LanguageI--------3               
1104----------English LanguageII-------3               
1106----------Hebrew LanguageI---------3             
1125----------Scientific Research------3             

第二个是显示具有等级的结果

SELECT 
    course.CourseNum, 
    course.CourseName,
    course.CreditHours,
    studentcoursecomplete.CourseGrade
FROM course, studentcoursecomplete
WHERE studentcoursecomplete.SID=1 
  And studentcoursecomplete.CourseID = course.CourseID 
  AND course.TypeID=1

结果是

Course Num---------Course Name----------Credit Hours-----Grade               
1101---------------Arabic Language-----------3------------60    
1103---------------English LanguageI---------3------------65        
1104---------------English LanguageII--------3------------70

所以现在我需要一个sql语句,显示第一条记录EXCEPT第二条记录!! sql语句必须显示此结果..

Course Num------Course Name---------Credit Hours                   
1106----------Hebrew LanguageI---------3                                       
1125----------Scientific Research------3                       

3 个答案:

答案 0 :(得分:0)

试试这段代码。它应该给你想要的结果。

SELECT course.CourseNum, course.CourseName, course.CreditHours
FROM course course left join
studentcoursecomplete studentcoursecomplete on
studentcoursecomplete.CourseID = course.CourseID  and studentcoursecomplete.SID=1
where studentcoursecomplete.CourseID is null
and course.TypeID=1;

答案 1 :(得分:0)

SELECT course.CourseNum, course.CourseName, course.CreditHours
FROM course
WHERE course.TypeID=1
MINUS
SELECT course.CourseNum, course.CourseName,course.CreditHours,studentcoursecomplete.CourseGrade
FROM course,studentcoursecomplete
WHERE studentcoursecomplete.SID=1 And studentcoursecomplete.CourseID = course.CourseID AND course.TypeID=1;

试试这个:)

答案 2 :(得分:0)

您可以执行左连接并查找NULL。在可能的情况下也使用表别名来使代码更具可读性。

select 
    c.CourseNum, 
    c.CourseName, 
    c.CreditHours
from course c
left join studentcoursecomplete cc
  on cc.CourseID = c.CourseID
  and cc.SID = 1
where c.TypeID=1
  and cc.CourseID is null