我需要在联接表上执行联接,但我不确定如何完成它。希望下面的查询能说明我要做的事情:获取国家名称、每个国家的领导人以及每个领导人的家乡。
我想这个查询的问题是在第二个join中使用了连接表“president”和president.homety廑id。我不知道还能尝试什么。

SELECT
  Countries.Name AS Country,
  President.Name AS Leader,
  PresidentHomeTown.Name AS LeaderHomeTown
FROM Countries
  LEFT OUTER JOIN PoliticalFigures AS President ON Countries.President_Id = President.Id
  LEFT OUTER JOIN Cities AS PresidentHomeTown ON President.HomeTown_Id = PresidentHomeTown.Id

在vs中,我得到一个错误,“多部分标识符”president.id“无法绑定。”
表和字段的名称是虚构的,但我需要解决一个相同的问题。我改了名字,使事情更清楚;希望这将与更多的人相关。
--更新--
也许原始代码有助于:
SELECT
    CaseComparisons.Directory AS CaseComparisonDir,
    BaselineResult.Directory AS BaselineResultDir,
    ComparisonResult.Directory AS ComparisonResultDir,
    Setup.FullSvnLink AS SvnLink,
    BaselineVersion.FullFilePath AS BaselineExecutableDir
FROM CaseComparisons
    LEFT OUTER JOIN Results AS BaselineResult ON CaseComparisons.BaselineResult_Id = Baseline.Id
    LEFT OUTER JOIN Results AS ComparisonResult ON CaseComparisons.ComparisonResult_Id = Comparison.Id
    LEFT OUTER JOIN Setups AS Setup ON Baseline.Setup_Id = Setups.Id
    LEFT OUTER JOIN BuildVersions AS BaselineVersion ON BaselineResult.Version_Id = BuildVersions.Id
WHERE
    CaseComparisons.Status = 'Queued' OR
    Baseline.Status = 'Queued' OR
    Comparison.Status = 'Queued'

运行查询时出现的错误:
The multi-part identifier "Baseline.Id" could not be bound.
The multi-part identifier "Comparison.Id" could not be bound.
The multi-part identifier "Baseline.Setup_Id" could not be bound.
The multi-part identifier "Setups.Id" could not be bound.
The multi-part identifier "BuildVersions.Id" could not be bound.
The multi-part identifier "Baseline.Status" could not be bound.

最佳答案

您指定的别名(例如在Results AS ComparisonResult中)与您尝试使用的别名(例如在Comparison.Id中)不匹配。所以,改变这个:

    LEFT OUTER JOIN Results AS ComparisonResult
                 ON CaseComparisons.ComparisonResult_Id = Comparison.Id

对此:
    LEFT OUTER JOIN Results AS ComparisonResult
                 ON CaseComparisons.ComparisonResult_Id = ComparisonResult.Id

或者这个:
    LEFT OUTER JOIN Results AS Comparison
                 ON CaseComparisons.ComparisonResult_Id = Comparison.Id

(其他所有连接也一样)。

10-05 19:58