【问题标题】:Subquery is not identifying column of main query子查询没有识别主查询的列
【发布时间】:2020-09-29 16:21:56
【问题描述】:

错误是

“查询错误 (1054):‘where 子句’中的未知列‘nt.id’”

这是查询

SELECT
  nt.from as 'SENDER EMP ID',
  if(nt.notification_time IS NOT NULL, nt.notification_time, nt.notification_start_time) as 'NOTIFICATION TIME',
  nt.delivery_type as 'NOTIFICATION TYPE',
  eh.first_name as 'SENDER FIRST NAME',
  eh.last_name as 'SENDER LAST NAME',
  eh.site_name as 'SENDER SITE NAME',
  nt.title as 'NOTIFICATION TITLE',
  nt.body as 'NOTIFICATION BODY',
  (
    select count(DISTINCT notifiable_id)
    from notifications.notifications
    where nt.id = notifications.notification_template_id
  ) as SEND TO No. OF USERS,
  (
    select GROUP_CONCAT('\n', counts_per_desig ) as counts
    from
    (
      select
        concat (if(quartz.employee_hierarchy.designation, quartz.employee_hierarchy.designation, "Un Assigned"), " : ", count(*) ) as counts_per_desig
      from notifications.notifications
      LEFT JOIN quartz.employee_hierarchy ON quartz.employee_hierarchy.employee_id = notifications.notifications.notifiable_id
      where notifications.notifications.notification_template_id  = nt.id
        AND notifications.read_at IS NOT NULL
      Group By quartz.employee_hierarchy.designation
    ) as 'READ BY No. OF USERS'
  )
FROM notifications.notification_template AS nt
LEFT JOIN quartz.employee_hierarchy as eh ON eh.employee_id = nt.from
where (nt.created_by_type = 1)
  and eh.location_id in (22, 123, 332)
  and nt.from not in (185994, 81016, 168090, 24799, 104967)

这部分有问题所以代码(子查询)

select GROUP_CONCAT('\n',counts_per_desig ) as counts
from
(
  select concat (if(quartz.employee_hierarchy.designation,quartz.employee_hierarchy.designation,"Un Assigned")," : ",count(*) ) as counts_per_desig
  from notifications.notifications
  LEFT JOIN quartz.employee_hierarchy ON quartz.employee_hierarchy.employee_id = notifications.notifications.notifiable_id
  where notifications.notifications.notification_template_id  = 123
    AND notifications.read_at IS NOT NULL
  Group By quartz.employee_hierarchy.designation
) as 'READ BY No. OF USERS'

【问题讨论】:

  • 附带说明:单引号是字符串文字的分隔符,而不是名称的分隔符。在 MySQL 中,您可以使用反引号作为名称,但最好的选择通常是完全不使用它们并使用没有问题的名称,例如read_by_number_of_users.
  • 另一个注意事项:用另一个值替换空值通常使用COALESCE 完成。所以if(nt.notification_time IS NOT NULL, nt.notification_time, nt.notification_start_time) as 'NOTIFICATION TIME' 变成了COALESCE(nt.notification_time nt.notification_start_time) as evaluated_notification_time
  • 另一边注:if(quartz.employee_hierarchy.designation, ...?您在这里将指定列视为布尔值。如果它是一个字符串,你应该将它与一些东西进行比较,以免在这里得到意外的结果。

标签: mysql sql subquery


【解决方案1】:

你的问题是你的层次太深了。不幸的是,无法在子子查询中访问列名。

因此,您必须将加入条件(通知模板 ID)上移一级。变化:

(
  select GROUP_CONCAT('\n', counts_per_desig ) as counts
  from
  (
    select
      concat (if(quartz.employee_hierarchy.designation, quartz.employee_hierarchy.designation, "Un Assigned"), " : ", count(*) ) as counts_per_desig
    from notifications.notifications
    LEFT JOIN quartz.employee_hierarchy ON quartz.employee_hierarchy.employee_id = notifications.notifications.notifiable_id
    where notifications.notifications.notification_template_id  = nt.id
      AND notifications.read_at IS NOT NULL
    Group By quartz.employee_hierarchy.designation
  ) as 'READ BY No. OF USERS'
)

(
  select group_concat('\n', counts_per_desig)
  from
  (
    select
      n.notification_template_id,
      concat(coalesce(ne.designation, 'unassigned'), ' : ', count(*)) as counts_per_desig
    from notifications.notifications n
    left join quartz.employee_hierarchy ne on ne.employee_id = n.notifiable_id
    where n.read_at is not null
    group by n.notification_template_id, ne.designation
  ) designations
  where designations.notification_template_id = nt.id
) as read_by_no_of_users

【讨论】:

  • Can't use count() with GROUP_CONCAT 即使我删除 count() 它也会返回多行,这给出了“子查询返回超过 1 行”跨度>
  • 你是对的。对不起。我已经更正了我的答案。另请参阅我根据您的要求做的笔记。
猜你喜欢
  • 1970-01-01
  • 2019-10-16
  • 2011-10-28
  • 1970-01-01
  • 2021-12-08
  • 2020-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多