我们知道,使用select count(*) from table1 where condition>100 group by ID时,结果中不会保留count为0的结果,如果我们需要保留count为0的结果怎么办?
不保留0的结果:
select GoodsTypeID,count(*) as Counts from tb_GoodsInfo
where GoodsPrice>1000
group by GoodsTypeID
保留0的结果:
select distinct a.GoodsTypeID,ISNULL(b.SubNum, 0) as Counts
from tb_GoodsInfo as a LEFT JOIN
(select GoodsTypeID,COUNT(1) as SubNum from tb_GoodsInfo as c
where c.GoodsPrice>1000
group by GoodsTypeID) as b
on a.GoodsTypeID = b.GoodsTypeID
本文介绍如何在SQL查询中处理count为0的情况,通过左连接和ISNULL函数实现保留count为0的结果,提供两种不同的查询方式。
1617

被折叠的 条评论
为什么被折叠?



