- 假设表table如下所示,
select name, count(1),count(*),count(age), count(distinct(age))
from table
group by name;
name |
age |
a | 14 |
a | 15 |
a | 15 |
b | null |
b | 16 |
c | 17 |
d | null |
e | '' |
返回的结果是:
a 3 3 3 2 b 2 2 1 1 c 1 1 1 1 d 1 1 0 0 e 1 1 1 1
一般情况下select count(*) 与select count(1)两者返回的结果一致。
但是针对表中主键的有无,情况会有不同。
无主键:count(1)比count(x)快
有主键:count(主键)最快
count(*)与count(1)都包括null统计,而count(column)不包括null统计