SELECT two.WorkOrderID,
te.EquipmentID,
tm.MaterialID,
CodMaterial,
MaterialName,
EquipmentName,
GoalPriceEnergyPerUnit,
pt.ParameterTypeID
FROM T_WorkOrders two
LEFT OUTER JOIN T_Equipment te
ON te.EquipmentID = two.EquipmentID
LEFT OUTER JOIN T_Materials tm
ON tm.MaterialID = two.MaterialID
LEFT JOIN T_Parameter_Type pt
ON ea.ParameterTypeID = pt.ParameterTypeID
WHERE WorkOrderNumber = 2
AND tm.MaterialID = 417
AND te.EquipmentID = 1076
AND ea.ParameterTypeID = 4918
答案 0 :(得分:0)
刚刚完成,评论中说了什么..
您已在查询中定义了表别名,如下所示
T_Equipment te
T_Materials tm
T_Parameter_Type pt
最后你引用的列的别名不属于上述任何一个..
ea.ParameterTypeID
所以请使用表所属的别名
来引用该列答案 1 :(得分:0)
您没有连接表,因为您使用了错误的别名。试着看看你的桌子
T_Equipment
MaterialID
T_WorkOrders
查看哪一个列可以与ParameterTypeID“连接”。 我无法帮助你,因为你没有提供足够的关于你的桌子的信息以及你真正想做的事情。但无论如何这是一个尝试
SELECT two.WorkOrderID,
te.EquipmentID,
tm.MaterialID,
CodMaterial,
MaterialName,
EquipmentName,
GoalPriceEnergyPerUnit,
pt.ParameterTypeID
FROM T_WorkOrders two
LEFT JOIN T_Equipment te
ON te.EquipmentID = two.EquipmentID
LEFT JOIN T_Materials tm
ON tm.MaterialID = two.MaterialID
LEFT JOIN T_Parameter_Type pt
( -- this is not correct syntax
ON te.ParameterTypeID = pt.ParameterTypeID
ON tm.ParameterTypeID = pt.ParameterTypeID
ON two.ParameterTypeID = pt.ParameterTypeID
)
WHERE WorkOrderNumber = 2
AND tm.MaterialID = 417
AND te.EquipmentID = 1076
AND ea.ParameterTypeID = 4918
以上代码是LEFT JOIN和ON的可能用途的示例,其中我连接不同的表。正如您在评论中看到的那样,这不起作用,因为代码有两个额外的ON-s。我想在这里做的就是向你展示你可能错过的组合。
此外,您在@TheGameiswar评论中提供的代码不正确。
SELECT two.WorkOrderID,
te.EquipmentID,
tm.MaterialID,
CodMaterial,
MaterialName,
EquipmentName,
GoalPriceEnergyPerUnit,
pt.ParameterTypeID
FROM T_WorkOrders two
LEFT JOIN T_Equipment te
ON te.EquipmentID = two.EquipmentID
LEFT JOIN T_Materials tm
ON tm.MaterialID = two.MaterialID
LEFT JOIN T_Parameter_Type pt
--ON is missing, maybe something like
-- pt.SomeID = two.SomeID
LEFT JOIN T_Equipment_Address ea
ON ea.ParameterTypeID=pt.ParameterTypeID
WHERE WorkOrderNumber=2
AND tm.MaterialID = 417
AND te.EquipmentID = 1076
AND pt.ParameterTypeID = 4918
它缺少ON语句,如代码注释中所示。试着解决这个问题。另外,使用Microsoft SQL Server Management Studio可以帮助您编写sql代码,因为如果您遗漏了某些内容,它会发出警告。此外,我有时会使用JOIN的视觉提醒来确定我在做什么,通常来自codinghorror。