简体   繁体   English

结合两个 SQL 查询

[英]combine two SQL queries

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;

How can I add Final_Exam_Level into the second query?如何将 Final_Exam_Level 添加到第二个查询中?

Final_Exam_Level is counting how many Exam_ID did each Student do. Final_Exam_Level 计算每个学生做了多少 Exam_ID。 Exam_Allocation has two foreign keys, S_Id and 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 ;

Just turn both of them into sub-queries of a higher query只需将它们都变成更高查询的子查询

 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM