3.3.4.8 Counting Rows数行

Databases are often used to answer the question, How often does a certain type of data occur in a table? For example, you might want to know how many pets you have, or how many pets each owner has, or you might want to perform various kinds of census operations on your animals.数据库通常用于回答“某类数据在表中出现的频率”的问题,例如,您可能想知道您有多少宠物,或者每个主人有多少宠物,或者您可能想对您的动物执行各种普查操作。

Counting the total number of animals you have is the same question as How many rows are in the pet table? because there is one record per pet. 计算你拥有的动物总数与“宠物表中有多少行?”的问题相同,因为每只宠物有一条记录。COUNT(*) counts the number of rows, so the query to count your animals looks like this:COUNT(*)计算行数,因此用于计算动物数的查询如下所示:

mysql> SELECT COUNT(*) FROM pet;
+----------+
| COUNT(*) |
+----------+
|        9 |
+----------+

Earlier, you retrieved the names of the people who owned pets. 早些时候,您检索了拥有宠物的人的姓名。You can use COUNT() if you want to find out how many pets each owner has:如果您想了解每个主人有多少宠物,可以使用COUNT()

mysql> SELECT owner, COUNT(*) FROM pet GROUP BY owner;
+--------+----------+
| owner  | COUNT(*) |
+--------+----------+
| Benny  |        2 |
| Diane  |        2 |
| Gwen   |        3 |
| Harold |        2 |
+--------+----------+

The preceding query uses GROUP BY to group all records for each owner. 前面的查询使用GROUP BY对每个owner的所有记录进行分组。The use of COUNT() in conjunction with GROUP BY is useful for characterizing your data under various groupings. COUNT()GROUP BY结合使用有助于在各种分组下表征数据。The following examples show different ways to perform animal census operations.以下示例显示了执行动物普查操作的不同方法。

Number of animals per species:每种动物的数量:

mysql> SELECT species, COUNT(*) FROM pet GROUP BY species;
+---------+----------+
| species | COUNT(*) |
+---------+----------+
| bird    |        2 |
| cat     |        2 |
| dog     |        3 |
| hamster |        1 |
| snake   |        1 |
+---------+----------+

Number of animals per sex:每种性别的动物数量:

mysql> SELECT sex, COUNT(*) FROM pet GROUP BY sex;
+------+----------+
| sex  | COUNT(*) |
+------+----------+
| NULL |        1 |
| f    |        4 |
| m    |        4 |
+------+----------+

(In this output, NULL indicates that the sex is unknown.)(在此输出中,NULL表示性别未知。)

Number of animals per combination of species and sex:每种物种和性别组合的动物数量:

mysql> SELECT species, sex, COUNT(*) FROM pet GROUP BY species, sex;
+---------+------+----------+
| species | sex  | COUNT(*) |
+---------+------+----------+
| bird    | NULL |        1 |
| bird    | f    |        1 |
| cat     | f    |        1 |
| cat     | m    |        1 |
| dog     | f    |        1 |
| dog     | m    |        2 |
| hamster | f    |        1 |
| snake   | m    |        1 |
+---------+------+----------+

You need not retrieve an entire table when you use COUNT(). 使用COUNT()时不需要检索整个表。For example, the previous query, when performed just on dogs and cats, looks like this:例如,前一个查询仅在猫和狗身上执行时,如下所示:

mysql> SELECT species, sex, COUNT(*) FROM pet
WHERE species = 'dog' OR species = 'cat'
GROUP BY species, sex;
+---------+------+----------+
| species | sex  | COUNT(*) |
+---------+------+----------+
| cat     | f    |        1 |
| cat     | m    |        1 |
| dog     | f    |        1 |
| dog     | m    |        2 |
+---------+------+----------+

Or, if you wanted the number of animals per sex only for animals whose sex is known:或者,如果您只想知道性别已知的动物的每种性别的动物数量:

mysql> SELECT species, sex, COUNT(*) FROM pet
WHERE sex IS NOT NULL
GROUP BY species, sex;
+---------+------+----------+
| species | sex  | COUNT(*) |
+---------+------+----------+
| bird    | f    |        1 |
| cat     | f    |        1 |
| cat     | m    |        1 |
| dog     | f    |        1 |
| dog     | m    |        2 |
| hamster | f    |        1 |
| snake   | m    |        1 |
+---------+------+----------+

If you name columns to select in addition to the COUNT() value, a GROUP BY clause should be present that names those same columns. 如果除了COUNT()值之外还指定了要选择的列,则应该存在一个GROUP BY子句来命名这些相同的列。Otherwise, the following occurs:否则,将发生以下情况:

See also Section 12.20.3, “MySQL Handling of GROUP BY”. 另请参见第12.20.3节,“分组的MySQL处理”See Section 12.20.1, “Aggregate Function Descriptions” for information about COUNT(expr) behavior and related optimizations.有关COUNT(expr)行为和相关优化的信息,请参阅第12.20.1节,“聚合函数描述”