结合两个SQL查询

时间:2011-05-02 20:52:59

标签: sql

SELECT Student.S_ID, COUNT(*) AS **Final_Exam_Level**
FROM Student, Exams, Exam_Allocation
WHERE (Student.S_ID)=Exam_Allocation.S_ID
  And ((Exams.Exam_ID)=Exam_Allocation.Exam_ID)
  And (Exams.Date_Taken) <= #12/31/2010#
GROUP BY Student.S_ID, Student.Course_Level
ORDER BY Student.S_ID;

SELECT Student.S_ID AS S_ID, Student.First_Name AS First_Name, Student.Surname AS Surname, MAX(New_Models.Date_Issued) AS Last_Course_Date, MAX(New_Models.Issue) AS Last_Issue, MAX(New_Models.Model_ID) AS Last_Model_ID, Student.Course_Level AS No_Training_Courses
FROM New_Models, New_Models_Allocation, Student
WHERE New_Models.Model_ID=New_models_Allocation.Model_ID And Student.S_ID=New_Models_Allocation.S_ID
GROUP BY Student.S_ID, Student.Course_Level, First_Name, Surname
ORDER BY MAX(New_Models.Model_ID) DESC;

如何将Final_Exam_Level添加到第二个查询中?

Final_Exam_Level正在计算每个学生所做的Exam_ID数量。 Exam_Allocation有两个外键,S_Id和Exam_ID

select Query_New_Models.*, Query_Exam.Final_Exam_Level
FROM (SELECT Student.S_ID AS S_ID, Student.First_Name AS First_Name, Student.Surname AS Surname, MAX(New_Models.Date_Issued) AS Last_Course_Date, MAX(New_Models.Issue) AS Last_Issue, MAX(New_Models.Model_ID) AS Last_Model_ID, Student.Course_Level AS No_Training_Courses
FROM New_Models, New_Models_Allocation, Student
WHERE New_Models.Model_ID=New_models_Allocation.Model_ID And Student.S_ID=New_Models_Allocation.S_ID
GROUP BY Student.S_ID, Student.Course_Level
ORDER BY MAX(New_Models.Model_ID) DESC)             INNER JOIN         (SELECT      Student.S_ID, COUNT(*) AS Final_Exam_Level
FROM Student, Exams, Exam_Allocation
WHERE (Student.S_ID)=Exam_Allocation.S_ID
And ((Exams.Exam_ID)=Exam_Allocation.Exam_ID)
And (Exams.Date_Taken)<=#12/31/2010#
GROUP BY Student.S_ID, Student.Course_Level
ORDER BY Student.S_ID
) ON  Query_Exam.S_ID = Query_New_Models.S_ID ;

1 个答案:

答案 0 :(得分:2)

只需将它们转换为更高查询的子查询

即可
 select Second.*, First.Final_Exam_Level
   from (Your Second Query Here) Second
            inner join
        (Your First Query Here) First    on First.Technician_ID = Second.Technician_ID