簡體   English   中英

子查詢未識別主查詢的列

[英]Subquery is not identifying column of main query

錯誤是

“查詢錯誤 (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'

你的問題是你太深了一層。 不幸的是,無法在子子查詢中訪問列名。

因此,您必須將加入條件(通知模板 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM