日期:2014-05-18  浏览次数:20349 次

请教count()
经常用到select   count(1)   from   .....
但是一直没明白为什么可以用count(1),表中也没有1这一列啊,count(2),count(3)   也一样都可以达到count(*)的效果,     各位位能具体解释这是什么意思吗?

------解决方案--------------------
重点是求满足后面条件的行数 ,括号里面是什么不重要
select * ,1 ,2, 'a ' from table
行数是一样的是吧
select Count(*) ,Count(1) ,Count(2),Count( 'a ') from table
还是一样的
明白了吧

------解决方案--------------------
相当于定义了一个常数列布局而已
------解决方案--------------------
增加一个常数列,select 1 from.....任何一个表
------解决方案--------------------
就是把这个1、2什么当做一个常数列,计算该常数列总数而已
------解决方案--------------------
declare @t table(date char(21))
insert @t select '1900-1-2 00:00:00.000 '
insert @t select '1900-1-2 00:00:00.001 '
insert @t select '1900-1-2 00:00:00.009 '
insert @t select '1900-1-2 00:00:00.002 '
insert @t select '1900-1-2 00:00:00.003 '
insert @t select '1900-1-2 00:00:00.004 '
insert @t select '1900-1-2 00:00:00.005 '
insert @t select '1900-1-2 00:00:00.006 '
insert @t select '1900-1-2 00:00:00.007 '
insert @t select '1900-1-2 00:00:00.008 '

select count(1) as '总行数 '
from @t


结果:
总行数
10

说明:1代表第一列,count(1)代表计算第一列有多少行。

峥狼(Staid Yang)