MySQL COUNT 函数

  • UNION 关键字

    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,则不会有任何区别。