任何人都可以帮我解决相关嵌套子查询的问题 即,当我试图在嵌套的子查询中使用祖父列时,我得到了这个错误
错误代码:1054 'where子句'中的未知列'scu.iUserId'
SELECT scu.iUserId, (SELECT SUM( sbs.`iNoPointsBeginning` + (SELECT COALESCE(SUM(BehaviorPts), 0) AS StudentPts FROM (SELECT ( COUNT(sbis.iIncidentSubmissionId) * sbi.iPointValue ) AS BehaviorPts FROM scn_behavior_incident_submission sbis JOIN scn_behavior_incident_actors sbia ON sbia.iIncidentSubmissionId = sbis.iIncidentSubmissionId LEFT JOIN scn_behavior_incidents sbi ON sbi.iIncidentId = sbis.iBehaviorIncidentId WHERE sbia.iUserId = scu.iUserId AND sbia.eActorType 'Witness' AND sbis.iSchoolId = '875' GROUP BY sbis.iBehaviorIncidentId) AS BehaviorTotal) ) AS stu_pt FROM scn_behavior_settings sbs WHERE sbs.`iSchoolId` = '875') FROM scn_sections_members AS scm INNER JOIN scn_users AS scu ON scu.iUserId = scm.iStudentId
答案 0 :(得分:2)
两个嵌套级别会导致此错误。内部子查询不“知道”在外部查询中定义的scu
。
尝试在没有内联子查询的情况下重写它。不确定这是否是正确的方法,但你会明白。 (sbs
表似乎与其他表无关,因此我将其设为CROSS JOIN
。如果存在关系则适当编辑):
SELECT
scu.iUserId,
sbs.iNoPointsBeginning
+ COUNT(sbis.iIncidentSubmissionId) * COALESCE(sbi.iPointValue, 0)
AS stu_pt
FROM
( SELECT
SUM(sbs.iNoPointsBeginning) AS iNoPointsBeginning
FROM
scn_behavior_settings sbs
WHERE sbs.iSchoolId = '97'
) AS sbs
CROSS JOIN
scn_sections_members AS scm
INNER JOIN scn_users AS scu
ON scu.iUserId = scm.iStudentId
LEFT JOIN
scn_behavior_incident_submission sbis
JOIN scn_behavior_incident_actors sbia
ON sbia.iIncidentSubmissionId = sbis.iIncidentSubmissionId
AND sbia.eActorType = 'Witness'
AND sbis.iSchoolId = '97'
LEFT JOIN scn_behavior_incidents sbi
ON sbi.iIncidentId = sbis.iBehaviorIncidentId
ON sbia.iUserId = scu.iUserId
GROUP BY scu.iUserId
, sbis.iBehaviorIncidentId
答案 1 :(得分:1)
如果你(暂时)删除那些混乱的子查询,你会看到你在说:
SELECT scu.iUserId
FROM scn_sections_members AS scm
INNER JOIN scn_users AS scu
ON scu.iUserId = scm.iStudentId
要让MySQL告诉您该列未知,它必须表示该列不存在。 scn_users的定义是什么?在该表中放置一个iUserId列,然后至少那部分问题将结束。