2014-11-04 72 views
0

如何連接4個表並檢索有關一個ID(行)的信息?使用SQL連接四個表

這是我到目前爲止有:

+1

什麼不工作你有什麼這麼遠嗎? – Undo 2014-11-04 14:32:18

+0

@Undo:我會說一切 – 2014-11-04 14:32:48

+0

您可以請分享表格結構,一些示例數據以及您試圖實現的結果嗎? – Mureinik 2014-11-04 14:33:12

回答

0

根據您的意見,問題是,你必須使用相同的名稱,這樣的東東用別名數列:

SELECT Crs_Desc, Enrollment.Enrollme, Enrollment.Offer_No 
FROM Offerin, Course 
JOIN Enrollment ON Enrollment.Offer_No = Offering.Offer_No 
JOIN Student ON Student.Std_ID = Enrollment.Std_ID 

WHERE Enrollment.Std_ID = '103' 
GROUP BY Enrollment.Std_ID; 

如果該查詢做不解決你的問題,分享你的表格數據和預期結果

+0

它是如何知道enr =登記表?那是我混亂的一部分 – extram 2014-11-04 14:44:04

+0

@extram你是什麼意思? Enrolment AS enr對錶進行別名,允許在整個查詢中將其作爲enr引用 – EyeOfTheHawks 2014-11-04 14:46:30

+0

此查詢中沒有聚合函數。 – Strawberry 2014-11-04 14:47:36

0

嘗試類似這樣的事情。

SELECT Student.Student_ID, Course.Crs_Desc, 
    Enrollment.Std_ID, Enrollment.Offer_No, 
    Offering.Offer_No 
    FROM Student 
     JOIN Enrollment 
      ON Student.Student_ID = Enrollment.Std_ID 
     JOIN Offering 
      ON Enrollment.Std_ID = Offering.Offer_No 
     JOIN Course 
      ON Offering.Offer_No = Course.CourseNo 
     WHERE Student_ID = '103'; 

此外,WHERE子句看起來可能是錯誤的。嘗試「WHERE Student_ID ='103';」

+0

我'爲此也得到一個錯誤。 ORA-00904:「學生」。「STUDENT_ID」:無效標識符 – extram 2014-11-04 14:55:45

+0

該列名稱是否正確? – 2014-11-04 14:57:29

+0

是的,我只是糾正了名稱,但這是我得到的錯誤ORA-00979:不是Course.Crs_Desc上的G​​ROUP BY表達式 – extram 2014-11-04 15:05:36

0

試試這個:

SELECT Student.Student_ID 
     ,Course.Crs_Desc 
     ,Enrollment.Std_ID 
     ,Enrollment.Offer_No 
     ,Offering.Offer_No 
    FROM Student 
     LEFT JOIN Enrollment 
      ON Student.Student_ID = Enrollment.Std_ID 
     LEFT JOIN Offering 
      ON Enrollment.Std_ID = Offering.Offer_No 
     LEFT JOIN Course 
      ON Offering.CourseNo = Course.CourseNo 
     WHERE Student.Student_ID = '103'; 
+0

錯誤在第10行: ORA-01722:無效數字星號on = Course.Course_No – extram 2014-11-04 15:28:18

+0

Offer.CourseNo和Course.CourseNo的數據類型是什麼? – Bacs 2014-11-04 15:29:55

+0

都是Varchar2 – extram 2014-11-04 15:33:45