如何在JOIN-ed表上执行JOIN?

时间:2012-01-25 20:48:39

标签: sql sql-server database

我需要在JOIN-ed表上执行JOIN,我不知道如何完成它。希望下面的查询能说明我正在尝试做什么:获取国家名称,每个国家的领导者以及每个领导者的家乡。

我认为此查询的问题是在与President.HomeTown_Id的第二次联接中使用JOIN-ed表“President”。我不知道还有什么可以尝试。

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.

3 个答案:

答案 0 :(得分:4)

您指定的别名(例如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

(同样适用于所有其他联接)。

答案 1 :(得分:0)

您在总统表中没有Id列。仔细检查一下。

答案 2 :(得分:0)

试试这个,你的名字不同:

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 = BaselineResult.Id 
    LEFT OUTER JOIN Results AS ComparisonResult ON CaseComparisons.ComparisonResult_Id = ComparisonResult.Id
    LEFT OUTER JOIN Setups AS Setup ON BaselineResult.Setup_Id = Setup.Id
    LEFT OUTER JOIN BuildVersions AS BaselineVersion ON BaselineResult.Version_Id = BaselineVersion.Id
WHERE 
    CaseComparisons.Status = 'Queued' OR 
    BaselineResult.Status = 'Queued' OR 
    ComparisonResult.Status = 'Queued'