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查詢是區分大小寫的,所以它並沒有任何區別,如果你給ZARA或Zara在where條件中。