簡體   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;

如何將 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 ;

只需將它們都變成更高查詢的子查詢

 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