count(1)=COUNT(*)=count(字段名) ------- 好像有點區別的...主要是null有問題 create table t (c1 int) insert into t select null insert into t select 2 insert into t select null insert into t select 3 insert into t select nullselect count(*),count(1),count(c1) from t drop table t--result:(可見count(列名)好像會排除null值) 5 5 2
一般用COUNT(*)或count(字段) 两者的区别是后者要忽略值为NULL的字段
count(1)=COUNT(*)<>count(字段名) 如果字段名 的字段可空,就不相等
--测试select count(*) as [*],count(1) as [1],count(a) as a from ( select 1 as a union all select 2 union all select null union all select 3 ) as t--结果 * 1 a ----------- ----------- ----------- 4 4 3(所影响的行数为 1 行)Warning: Null value is eliminated by an aggregate or other SET operation.
count(1)=count(*)
从性能上来说
count(1)=count(*)=count(主健)但绝没有 count(1)=count(*)=count(字段)的说法.
首先效率不同,count(1)和count(字段)一样是最快的,×最慢,
其次意义不同
-------
好像有點區別的...主要是null有問題
create table t (c1 int)
insert into t select null
insert into t select 2
insert into t select null
insert into t select 3
insert into t select nullselect count(*),count(1),count(c1) from t
drop table t--result:(可見count(列名)好像會排除null值)
5 5 2
两者的区别是后者要忽略值为NULL的字段
如果字段名 的字段可空,就不相等
from (
select 1 as a
union all select 2
union all select null
union all select 3
) as t--结果
* 1 a
----------- ----------- -----------
4 4 3(所影响的行数为 1 行)Warning: Null value is eliminated by an aggregate or other SET operation.
count(*)会自动按主健或第一个索引进行索引扫描.
count(普通字段)是最慢的,表扫描.
首先效率不同,count(1)和count(字段)一样是最快的,*最慢,
其次意义不同
一般用COUNT(*)或count(字段)
两者的区别是后者要忽略值为NULL的字段count(1)=COUNT(*)<>count(字段名)
如果字段名 的字段可空,就不相等
xray2005(爱汤) ( ) 信誉:100 2006-08-14 11:27:00 得分: 0
我来综合一下:个人认为count(1)=COUNT(*)=count(字段名)这个是不能完全相等的,
首先效率不同,count(1)和count(字段)一样是最快的,*最慢,
其次意义不同
一般用COUNT(*)或count(字段)
两者的区别是后者要忽略值为NULL的字段count(1)=COUNT(*)<>count(字段名)
如果字段名 的字段可空,就不相等
-----------------------------------------------看看我上面的话再发表总结.