用SQL连接四个表

时间:2014-11-04 14:31:33

标签: mysql oracle

如何加入4个表并检索有关一个ID(行)的信息?

这是我到目前为止所做的:

3 个答案:

答案 0 :(得分: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;

如果此查询无法解决您的问题,请分享您的表格数据和预期结果

答案 1 :(得分: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&#39 ;;"

答案 2 :(得分: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';