【发布时间】: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, ...?您在这里将指定列视为布尔值。如果它是一个字符串,你应该将它与一些东西进行比较,以免在这里得到意外的结果。