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。