1

我有 3 张桌子:单位类型派系

Units 具有来自 Types 和 Factions(TypeID 和 FactionID)的外键。我需要使用 Types.TypeName 和 Factions.FactionName 来获取 Units.UnitName。

我想我可能需要加入,但我不确定如何构建它。有什么帮助吗?

编辑:这是相关表格的截图:http: //i.imgur.com/Il9Qn.png

我需要返回这些数据: UnitName - SupplyCost - CreditsCost - MetalCost - CrystalCost

获取此信息的表单将提供 FactionName 和 TypeName,因为它们是文本值,因此用户易于理解,而不是数字 ID。

4

4 回答 4

3
SELECT Units.UnitName
FROM Units INNER JOIN Types ON Units.TypeId = Types.TypeId
    INNER JOIN Factions ON Units.FactionId = Factions.FactionId
WHERE Types.TypeName = @typeName
    AND Factions.FactionName = @factionName

那会是你所追求的吗?如果没有,您可能需要在问题中提供更多信息。

于 2012-04-26T09:48:19.357 回答
3

就像是:

select UnitName, SupplyCost, CreditsCost, MetalCost, CrystalCost from Units
inner join Types on Types.TypeID = Units.TypeID 
inner join Factions on Faction.FactionID = Units.FactionID
WHERE Types.TypeName = @typeName
AND Factions.FactionName = @factionName
于 2012-04-26T09:48:56.807 回答
2

请检查一下

select UnitName, SupplyCost, CreditsCost, MetalCost, CrystalCost from Units
inner join Types on Types.TypeID = Units.TypeID 
inner join Factions on Faction.FactionID = Units.FactionID
WHERE Types.TypeName = @typeName
AND Factions.FactionName = @factionName
于 2012-04-26T09:48:28.007 回答
2
SELECT
   SupplyCost, CreditsCost, MetalCost, CrystalCost
FROM
   Units 
      INNER JOIN Types ON Units.TypeID = Types.TypeId
      INNER JOIN Factions ON Units.FactionId = Factions.TypeId
WHERE 
      Types.TypeName = 'value from form for types'
      AND
      Factions.FactionName = 'value from form for factions'

您可以阅读http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html以获得关于 JOIN 的直观解释

于 2012-04-26T09:48:41.350 回答