MySQL Count()函数
MySQL Count()函数是用于统计记录数的数量的最简单的函数, 这是由 SELECT 语句返回。
理解 Count() 函数,考虑一个 employee_tbl 表,它是具有以下记录:
mysql> SELECT * FROM employee_tbl; +------+------+------------+--------------------+ | id | name | work_date | daily_typing_pages | +------+------+------------+--------------------+ | 1 | John | 2007-01-24 | 250 | | 2 | Ram | 2007-05-27 | 220 | | 3 | Jack | 2007-05-06 | 170 | | 3 | Jack | 2007-04-06 | 100 | | 4 | Jill | 2007-04-06 | 220 | | 5 | Zara | 2007-06-06 | 300 | | 5 | Zara | 2007-02-06 | 350 | +------+------+------------+--------------------+ 7 rows in set (0.00 sec)
现在假定在上表的基础上计算这个表中的行总数,那么如下所示:
mysql>SELECT COUNT(*) FROM employee_tbl ; +----------+ | COUNT(*) | +----------+ | 7 | +----------+ 1 row in set (0.01 sec)
同样,如果要计算 Zara 记录数,那么如下:
mysql>SELECT COUNT(*) FROM employee_tbl -> WHERE name="Zara"; +----------+ | COUNT(*) | +----------+ | 2 | +----------+ 1 row in set (0.04 sec)
注:所有的SQL查询是不区分大小写,所以它没有任何区别,如果在WHERE条件给定ZARA 或 Zara。