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:否则,将发生以下情况:
If the 如果启用了ONLY_FULL_GROUP_BY
SQL mode is enabled, an error occurs:ONLY_FULL_GROUP_BY
模式,则会发生错误:
mysql>SET sql_mode = 'ONLY_FULL_GROUP_BY';
Query OK, 0 rows affected (0.00 sec) mysql>SELECT owner, COUNT(*) FROM pet;
ERROR 1140 (42000): In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'menagerie.pet.owner'; this is incompatible with sql_mode=only_full_group_by
If 如果未启用ONLY_FULL_GROUP_BY
is not enabled, the query is processed by treating all rows as a single group, but the value selected for each named column is nondeterministic. ONLY_FULL_GROUP_BY
,则通过将所有行视为单个组来处理查询,但为每个命名列选择的值是不确定的。The server is free to select the value from any row:服务器可以从任意行中自由选择值:
mysql>SET sql_mode = '';
Query OK, 0 rows affected (0.00 sec) mysql>SELECT owner, COUNT(*) FROM pet;
+--------+----------+ | owner | COUNT(*) | +--------+----------+ | Harold | 8 | +--------+----------+ 1 row in set (0.00 sec)
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(
behavior and related optimizations.expr
)COUNT(
行为和相关优化的信息,请参阅第12.20.1节,“聚合函数描述”。expr
)