我收到错误 #1054 - 'on 子句' 中的未知列 'e.ProjectID' 使用以下 SQL
SELECT ProjectID
FROM Project as p
INNER JOIN (SELECT EquipID , EquipName , EquipTypeID , (EquipPrice*Quantity) As CstAmount FROM Equipment) as e
ON p.ProjectID=e.ProjectID
我收到错误 #1054 - 'on 子句' 中的未知列 'e.ProjectID' 使用以下 SQL
SELECT ProjectID
FROM Project as p
INNER JOIN (SELECT EquipID , EquipName , EquipTypeID , (EquipPrice*Quantity) As CstAmount FROM Equipment) as e
ON p.ProjectID=e.ProjectID
您的子查询没有将该列暴露给外部查询。
SELECT ProjectID
FROM Project As p
INNER
JOIN (
SELECT ProjectID
, EquipID
, EquipName
, EquipTypeID
, (EquipPrice*Quantity) As CstAmount
FROM Equipment
) As e
ON p.ProjectID = e.ProjectID
但是,如果您只想返回 ProjectID,那么我建议您改为执行此查询:
SELECT ProjectID
FROM Project
WHERE EXISTS (
SELECT *
FROM Equipment
WHERE ProjectID = Project.ProjectID
)
您不需要任何子查询。只需将项目表与设备表连接起来,如下所示
SELECT p.ProjectID,e.EquipID ,e.EquipName ,e.EquipTypeID ,e.EquipPrice* e.Quantity as CstAmount from Project P join Equipment e on p.ProjectID=e.ProjectID
你可以在没有子查询的情况下做到这一点
SELECT p.ProjectID ,e.EquipID , e.EquipName , e.EquipTypeID , (e.EquipPrice * e.Quantity) As CstAmount
FROM Project as p
INNER JOIN Equipment as e
ON p.ProjectID= e.ProjectID
你想做什么?您的查询可以重写如下,但我怀疑这是您想要的
SELECT p.ProjectID
FROM Project as p
INNER JOIN Equipment as e
ON p.ProjectID = e.ProjectID
您是否还需要选择子查询的字段?