表的结构如下:
create table mytable
(
id int not null,
[name] varchar(10) null
)
表的记录如下:

--以下是正确的统计sql语句如下:
select count(id) as 行数 from mytable --统计结果为2
--以下sql语句也可执行正确的统计结果,即使有些字段在表中没有.
select count('ddd') as 行数 from mytable --统计结果为2
select count('') as 行数 from mytable --统计结果为2
select count(' ') as 行数 from mytable --统计结果为2
select count('dfd') as 行数 from mytable --统计结果为2
select count(1/2) as 行数 from mytable --统计结果为2
--以下sql语句统计结果不准确,不提倡写.
select count([name]) as 行数 from mytable --统计结果为1,原因是name有空值.
总结:对于有空值的列,我们是不用来统计的;表中没有的字段也可同样做到正确的统计结果.
本文详细介绍了使用SQL进行行数统计的方法,包括如何避免因空值导致的计数不准确问题,并分享了一些实用的计数技巧。

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



