【问题标题】:sub-query not accessing parent query alias子查询不访问父查询别名
【发布时间】:2018-04-05 07:41:59
【问题描述】:
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`

在这个子查询中它返回错误,“'where 子句'中的未知列'schemeId'”。在 mysql 子查询中访问第三级子查询中的父参数?

(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....) 中放置另一个子查询,我会提到它会给我错误。

【问题讨论】:

  • 你在这里犯了一个错误scheme_id=schemeIdbatch_id=batchId 在右手边它不应该是schemeId。它应该是您要比较的实际值。
  • 我想将 schemeId 从 thi 选择器 batch.scheme_id as schemeId 传递给子查询。
  • 查看下面的答案

标签: mysql subquery


【解决方案1】:

scheme_id=schemeId

应该是

scheme_id=batch.scheme_id

batch_id=batchId

应该是

batch_id=batch.batch_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`

【讨论】:

  • 没有加入你不能使用批处理表它给出错误Unknown column 'batch.scheme_id' in 'where clause'。如果我们加入批处理表,那么我们没有来自父选择器的任何引用,结果将不会按我们想要的方式返回。
【解决方案2】:

'据我所知,您将无法传递“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`

【讨论】:

  • 然后尝试在 where 子句中使用另一个子查询。其中mark_attendance="p" AND batch_id=batchId` AND scheme_id in(从'batch'中选择不同的scheme_id
  • 希望有帮助
  • 你能修改上面的答案吗?
  • 这对您有帮助吗?
【解决方案3】:

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

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`

希望对其他人有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多