我知道我很亲密,它正在杀了我.这里需要一双新鲜的眼睛……
SELECT
first_name,
COUNT(*) FROM
(SELECT first_name, job_status FROM typesetting
LEFT JOIN employees ON typesetting.employees_id = employees.id
LEFT JOIN job_status ON typesetting.job_status_id = job_status.id
WHERE job_status NOT LIKE 'Archived'
ORDER BY first_name ASC)
AS CNT
GROUP BY first_name
这让我:
>阿诺德(5)
>克劳迪娅(19)
>珍妮特(29)
> Rich(21)
>汤姆(4)
>未分配(24)
但是,有一些结果,我在结果中非常想要零值,如下所示:
>阿诺德(5)
>谢丽尔(0)
>克劳迪娅(19)
> Cyndi(0)
>珍妮特(29)
>菲尔(0)
> Rich(21)
>汤姆(4)
>未分配(24)
我究竟做错了什么?很近!谢谢!
-Matt
最佳答案 在INNER查询中,您将过滤掉所有人,这可能会给您0个结果.
试试这个:
SELECT first_name,
sum(CASE WHEN job_status = 'Archived' THEN 1 ELSE 0 END) AS cnt
FROM typesetting
LEFT JOIN employees ON typesetting.employees_id = employees.id
LEFT JOIN job_status ON typesetting.job_status_id = job_status.id
GROUP BY first_name
ORDER BY first_name ASC;