本文介绍了当Count(*)结果为Null时,返回1而不是0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从SQL Server获得的代码
My code from SQL Server:
SELECT ESTAGIO.SK_ESTAGIO, ISNULL(count(ESTAGIO.SK_ESTAGIO), 0) as how_many
from ESTAGIO
left join ESTAGIARIO
on ESTAGIARIO.SK_ESTAGIO = ESTAGIO.SK_ESTAGIO
group by
ESTAGIO.SK_ESTAGIO
当 ESTAGIARIO表中不存在 ESTAGIO.SK_ESTAGIO时,它返回1而不是0,我已经尝试使用ISNULL(), NULLIF()和COALESCE()仍然找不到上面的查询在应为0时返回1的问题。
When "ESTAGIO.SK_ESTAGIO" doesn't exist in the table "ESTAGIARIO" it returns 1 instead of 0, I already tried to use ISNULL(), NULLIF() and COALESCE() and still couldn't find the problem that is making the query above returning 1 when it should be 0.
推荐答案
您正在计算错误的字段。这样做,从外部联接表 ESTAGIARIO
中获取字段(不是从 ESTAGIO
中获取):
You are counting the wrong field. Do it like this, taking the field from the outer joined table ESTAGIARIO
(not from ESTAGIO
):
SELECT ESTAGIO.SK_ESTAGIO, Count(ESTAGIARIO.SK_ESTAGIO) as how_many
from ESTAGIO
left join ESTAGIARIO
on ESTAGIARIO.SK_ESTAGIO = ESTAGIO.SK_ESTAGIO
group by
ESTAGIO.SK_ESTAGIO
顺便说一句,。
BTW, count
can never return null
.
这篇关于当Count(*)结果为Null时,返回1而不是0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!