具有多个左连接的SQL查询难度

时间:2012-04-05 07:14:34

标签: sql database

我有一个查询

SELECT * FROM Issue I, Condition C, Result R, Status S      
            LEFT JOIN Issue as i1 ON C.ID = i1.ConditionID
            LEFT JOIN Issue as i2 ON R.ID = i2.ResultID
            LEFT JOIN Issue as i3 ON S.ID = i3.StatusID 
            WHERE I.ID = 452

这给了我错误信息:

  

无法绑定多部分标识符“C.ID”   无法绑定多部分标识符“R.ID”。

我想得到如下结果:

  

ID | IssueName | ConditionID | ResultID | StatusID | ID | ConditionName | ID | ResultName | ID | StatusName

5 个答案:

答案 0 :(得分:2)

我试试

SELECT *
FROM Issue I
LEFT JOIN Condition C ON C.ID = I.ConditionID
LEFT JOIN Result    R ON R.ID = I.ResultID
LEFT JOIN Status    S ON S.ID = I.StatusID
WHERE I.ID = 452

答案 1 :(得分:2)

我以为您没有使用MySQL Server。试试这个:

SELECT * 
FROM Issue I, Condition C, Result R, Status S      
     LEFT JOIN Issue as i1 ON C.ID = i1.[Condition ID]
     LEFT JOIN Issue as i2 ON R.ID = i2.[Result ID]
     LEFT JOIN Issue as i3 ON S.ID = i3.StatusID 
WHERE I.ID = 452

SELECT *
FROM Issue I LEFT JOIN Condition C ON C.ID = I.ConditionID
     LEFT JOIN Result    R ON R.ID = I.ResultID
     LEFT JOIN Status    S ON S.ID = I.StatusID
WHERE I.ID = 452

答案 2 :(得分:1)

SELECT * FROM Issue I, Condition C, Result R , Status S      
                                             ^---- You missing this
            LEFT JOIN Issue as i1 ON C.ID = i1.Condition ID
            LEFT JOIN Issue as i2 ON R.ID = i2.Result ID
            LEFT JOIN Issue as i3 ON S.ID = i3.StatusID 
            WHERE I.ID = 452

答案 3 :(得分:1)

首先,在你发布的查询中,关系“我”和任何其他关系之间没有联接,所以你最终会在这里加入笛卡尔联盟。

使用旧式加入查询可能如下所示:

SELECT *
  FROM Issue I, Condition C, Result R, Status S
 WHERE I.ConditionID = C.ID
   AND I.ResultID = R.ID
   AND I.StatusID = S.ID
   -- the filter comes here
  AND I.ID = 42;

使用ANSI连接语法:

SELECT *
  FROM Issue I
    LEFT JOIN Condition C ON C.ID = I.ConditionID
    LEFT JOIN Result R ON R.ID = I.ResultID
    LEFT JOIN Status S ON S.ID = I.StatusID
 -- the filter comes here
 WHERE I.ID = 42;

答案 4 :(得分:0)

您的查询中有拼写错误:

Result R [comma is needed here] Status S