子查询不访问父查询别名

时间:2018-04-05 07:41:59

标签: mysql subquery

SELECT *,
`batch`.`batch_id` as `batchId`,
`batch`.`center_id` as `centerId`,
`batch`.`scheme_id` as `schemeId`,
`batch`.`batch_start_date` as `BatchStartDate`,
(SELECT COUNT(`attendance_count`) AS `eligible_for_assessment` FROM 
     (SELECT COUNT(`mark_attendance`) AS `attendance_count` FROM `dailyattendance` 
      WHERE `mark_attendance`="p" AND `scheme_id`=`schemeId` AND `batch_id`=`batchId`
      AND `center_id`=`centerId` GROUP BY `candidate_id`) AS `sfkjsd`
      WHERE `attendance_count` > 6
)
from `batch`

在此子查询中,它返回错误,"未知列' schemeId'在' where子句'"。在mysql子查询中访问第3级子查询中的父参数?

(SELECT COUNT(`attendance_count`) AS `eligible_for_assessment` FROM 
     (SELECT COUNT(`mark_attendance`) AS `attendance_count` FROM `dailyattendance` 
      WHERE `mark_attendance`="p" AND `scheme_id`=1 AND `batch_id`=2
      AND `center_id`=3 GROUP BY `candidate_id`) AS `sfkjsd`
      WHERE `attendance_count` > 6
)

此查询返回一个月内出现超过6天的候选人总数。我需要通过这个" schemeId"动态来自父选择查询。

(SELECT COUNT(*) from `enrolment` WHERE
`enrolment`.`batch_id`=`batchId` AND `enrolment`.`center_id`=`centerId`
AND `enrolment`.`ew_enrolled_for_scheme`=`schemeId`) as `enrolled_student_count`

在这个查询中我正在使用父选择器schemeId和其他,它让我计算结果。但是,如果我在上面的子查询中在FROM (SELECT....)内放置另一个子查询,我提到它会给我错误。

3 个答案:

答案 0 :(得分:0)

scheme_id = schemeId

应该是

scheme_id = batchscheme_id

batch_id = batchId

应该是

batch_id = batchbatch_id

试试这段代码

SELECT *,
`batch`.`batch_id` as `batchId`,
`batch`.`center_id` as `centerId`,
`batch`.`scheme_id` as `schemeId`,
`batch`.`batch_start_date` as `BatchStartDate`,
(SELECT COUNT(`attendance_count`) AS `eligible_for_assessment` FROM 
     (SELECT COUNT(`mark_attendance`) AS `attendance_count` FROM `dailyattendance` 
      WHERE `mark_attendance`="p" AND `scheme_id`=`batch`.`scheme_id` AND `batch_id`=`batch`.`batch_id`
      AND `center_id`=`centerId` GROUP BY `candidate_id`) AS `sfkjsd`
      WHERE `attendance_count` > 6
)
from `batch`

答案 1 :(得分:0)

'就我所知,你将无法通过“schemeId”。尝试在where子句中使用另一个子查询。

    SELECT *,
    `batch`.`batch_id` as `batchId`,
    `batch`.`center_id` as `centerId`,
    `batch`.`scheme_id` as `schemeId`,
    `batch`.`batch_start_date` as `BatchStartDate`,
    (SELECT COUNT(`attendance_count`) AS `eligible_for_assessment` 
    FROM 
         (SELECT COUNT(`mark_attendance`) AS `attendance_count` 
          FROM `dailyattendance` a 
          WHERE `mark_attendance`="p" AND `batch_id`=`batchId`
          AND `scheme_id` in (select `scheme_id` from `batch`) 
          AND `center_id`=`centerId` 
          GROUP BY `candidate_id`) AS `sfkjsd`
    WHERE `attendance_count` > 6)
    from `batch`

答案 2 :(得分:0)

是的,我们无法在第三级子查询中传递第一级别名。您需要在第三级选择列,然后比较到第二级。

SELECT *,
`batch`.`scheme_id` as `schemeId`,
`batch`.`center_id` as `centerId`,
`batch`.`batch_id` as `batchId`,
(SELECT COUNT(`attendance_count`) AS `eligible_for_assessment` FROM 
     (SELECT COUNT(`mark_attendance`) AS `attendance_count`,
      `center_id` AS `daCenterId`, `batch_id` AS `daBatchId`,
      `scheme_id` AS `daSchemeId` FROM `dailyattendance`
      WHERE `mark_attendance`="p" GROUP BY `candidate_id`)
      AS `sfkjsd`
WHERE `attendance_count` > 6 AND `daCenterId`=`centerId`
AND `daBatchId`=`batchId` AND `daSchemeId`=`schemeId`)
FROM `batch`

希望对其他人有帮助!