MySQL中的相关子查询无法在main where stmt中包含“column”

MySQL中的相关子查询无法在main where stmt中包含“column”,mysql,mariadb,Mysql,Mariadb,使用下面的查询,包括一个相关的子查询,我希望能够从主查询中找到记录,其中PhotoCount是子查询>即>1或>5 我已经试过了 其中PhotoCount> 其中,从tblëU照片p中选择COUNToperatorID,其中p.operatorID=aoh.operatorID和p.imgëU creator='Daniël E.Cronk'> 然而,所有这些都会产生未知列错误 我尝试添加@PhotoCount:=然后在WHERE PhotoCount中进行计算,但这不会产生任何结果 SELEC

使用下面的查询,包括一个相关的子查询,我希望能够从主查询中找到记录,其中PhotoCount是子查询>即>1或>5

我已经试过了

其中PhotoCount> 其中,从tblëU照片p中选择COUNToperatorID,其中p.operatorID=aoh.operatorID和p.imgëU creator='Daniël E.Cronk'>

然而,所有这些都会产生未知列错误

我尝试添加@PhotoCount:=然后在WHERE PhotoCount中进行计算,但这不会产生任何结果

SELECT af.frameID, af.msn AS afMSN, af.ln AS afLN, af.aircraft_type AS toestelA, af.aircraft_cat AS afAircraftCat, af.airframe_status, aoh.operatorID, aoh.aircraftframeID, aoh.msn, aoh.ln, aoh.lvmID, aoh.toestel, aoh.aircraft_cat, aoh.reg, aoh.delivered, aoh.delivery_comment, aoh.engines, aoh.configuration, aoh.fleet_number, aoh.aircraft_name, aoh.special_colors, aoh.comments, aoh.exit_date, aoh.last_updated, lvm.luchtvaartmaatschappijID AS lvmid, lvm.luchtvaartmaatschappij AS lvmnaam, lvm.sm_logo, (SELECT COUNT(operatorID) FROM tbl_photos p WHERE p.operatorID =  aoh.operatorID AND p.img_creator = 'Daniël E. Cronk') AS PhotoCount

            FROM tbl_aircraft_frame af

            LEFT JOIN tbl_aircraft_operator_history aoh
            ON af.msn = aoh.msn AND af.aircraft_cat = aoh.aircraft_cat

            LEFT JOIN tbl_toestel t
            ON af.aircraft_cat = t.toestelID

            LEFT JOIN tbl_luchtvaartmaatschappij lvm
            ON aoh.lvmID = lvm.luchtvaartmaatschappijID

如果要使用选择列表中的列,则需要使用HAVING而不是WHERE


如果要使用选择列表中的列,则需要使用HAVING而不是WHERE


欢迎来到堆栈溢出。请学习如何使用堆栈溢出,并阅读如何提高问题的质量。然后查看SQL相关问题。并简化问题欢迎使用堆栈溢出。请学习如何使用堆栈溢出,并阅读如何提高问题的质量。然后查看SQL相关问题。并简化问题谢谢。我从来不知道这是可能的。感谢您的回复。为了加快查询速度,请将查询从内到外-查找计数大于10的照片的ID,然后加入其他所有内容。谢谢。我从来不知道这是可能的。感谢您的回复。为了提高速度,请将查询由内而外-查找计数大于10的照片的ID,然后加入其他所有内容。
SELECT 
    af.frameID,
    af.msn AS afMSN,
    af.ln AS afLN,
    af.aircraft_type AS toestelA,
    af.aircraft_cat AS afAircraftCat,
    af.airframe_status,
    aoh.operatorID,
    aoh.aircraftframeID,
    aoh.msn,
    aoh.ln,
    aoh.lvmID,
    aoh.toestel,
    aoh.aircraft_cat,
    aoh.reg,
    aoh.delivered,
    aoh.delivery_comment,
    aoh.engines,
    aoh.configuration,
    aoh.fleet_number,
    aoh.aircraft_name,
    aoh.special_colors,
    aoh.comments,
    aoh.exit_date,
    aoh.last_updated,
    lvm.luchtvaartmaatschappijID AS lvmid,
    lvm.luchtvaartmaatschappij AS lvmnaam,
    lvm.sm_logo,
    (SELECT 
            COUNT(operatorID)
        FROM
            tbl_photos p
        WHERE
            p.operatorID = aoh.operatorID
                AND p.img_creator = 'Daniël E. Cronk') AS PhotoCount
FROM
    tbl_aircraft_frame af
        LEFT JOIN
    tbl_aircraft_operator_history aoh ON af.msn = aoh.msn
        AND af.aircraft_cat = aoh.aircraft_cat
        LEFT JOIN
    tbl_toestel t ON af.aircraft_cat = t.toestelID
        LEFT JOIN
    tbl_luchtvaartmaatschappij lvm ON aoh.lvmID = lvm.luchtvaartmaatschappijID
HAVING PhotoCount > 10