SQL COUNT 函数是最简单的函数,在计算预计由SELECT语句返回的记录数时非常有用。
要了解 COUNT 函数,请考虑一个 employee_tbl 表,该表具有以下记录-
SQL> 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)
现在,假设基于上表您要计算该表中的总行数,则可以按照以下步骤进行操作:
SQL>SELECT COUNT(*) FROM employee_tbl ; +----------+ | COUNT(*) | +----------+ | 7 | +----------+ 1 row in set (0.01 sec)
同样,如果您要计算Zara的记录数,则可以按以下步骤进行操作-
SQL>SELECT COUNT(*) FROM employee_tbl -> WHERE name="Zara"; +----------+ | COUNT(*) | +----------+ | 2 | +----------+ 1 row in set (0.04 sec)
注意-所有SQL查询都不区分大小写,因此,如果在WHERE CONDITION中输入ZARA或Zara,则不会有任何区别。
参考链接
https://www.learnfk.com/sql/sql-count-function.html
标签:COUNT,Function,06,无涯,+----------+,2007,SQL,Zara From: https://blog.51cto.com/u_14033984/9292148