赞
踩
执行效果:
1. count(1) and count(*)
实例分析
- mysql> create table counttest(name char(1), age char(2));
- Query OK, 0 rows affected (0.03 sec)
-
- mysql> insert into counttest values
- -> ('a', '14'),('a', '15'), ('a', '15'),
- -> ('b', NULL), ('b', '16'),
- -> ('c', '17'),
- -> ('d', null),
- ->('e', '');
- Query OK, 8 rows affected (0.01 sec)
- Records: 8 Duplicates: 0 Warnings: 0
-
- mysql> select * from counttest;
- +------+------+
- | name | age |
- +------+------+
- | a | 14 |
- | a | 15 |
- | a | 15 |
- | b | NULL |
- | b | 16 |
- | c | 17 |
- | d | NULL |
- | e | |
- +------+------+
- 8 rows in set (0.00 sec)
-
- mysql> select name, count(name), count(1), count(*), count(age), count(distinct(age))
- -> from counttest
- -> group by name;
- +------+-------------+----------+----------+------------+----------------------+
- | name | count(name) | count(1) | count(*) | count(age) | count(distinct(age)) |
- +------+-------------+----------+----------+------------+----------------------+
- | a | 3 | 3 | 3 | 3 | 2 |
- | b | 2 | 2 | 2 | 1 | 1 |
- | c | 1 | 1 | 1 | 1 | 1 |
- | d | 1 | 1 | 1 | 0 | 0 |
- | e | 1 | 1 | 1 | 1 | 1 |
- +------+-------------+----------+----------+------------+----------------------+
- 5 rows in set (0.00 sec)
额外参考资料:http://blog.csdn.net/lihuarongaini/article/details/68485838
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。