• 计算行

    计算行

    数据庫通常被用于回答问题,“在一个表中,特定的数据有多少条?”,例如,你可能想知道你有多少宠物,或者,每个宠物拥有者有多少只宠物,或者,在普查中,你可能想知道有多少种宠物。

    计算你的宠物数量与“在你的宠物表中有多少行”是同样一个问题,因为每个宠物都有一个记录。COUNT(*)可以计算行的数量,所以,统计你宠物数量可以这样:

    1. mysql> SELECT COUNT(*) FROM pet;
    2. +----------+
    3. | COUNT(*) |
    4. +----------+
    5. | 9 |
    6. +----------+

    早期,你查询人的名字,及此人所有宠物的数量,你会使用COUNT()来找到拥有者的宠物数量:

    1. mysql> SELECT owner, COUNT(*) FROM pet GROUP BY owner;
    2. +--------+----------+
    3. | owner | COUNT(*) |
    4. +--------+----------+
    5. | Benny | 2 |
    6. | Diane | 2 |
    7. | Gwen | 3 |
    8. | Harold | 2 |
    9. +--------+----------+

    前面的查询使用GROUP BY来对每个拥有者的记录进行分组。COUNT()和GROUP BY的组合使用对于各种数据分组是很有用的,以下例子显示了执行宠物普查操作的不同方式:

    每个物种的动物数量:

    1. mysql> SELECT species, COUNT(*) FROM pet GROUP BY species;
    2. +---------+----------+
    3. | species | COUNT(*) |
    4. +---------+----------+
    5. | bird | 2 |
    6. | cat | 2 |
    7. | dog | 3 |
    8. | hamster | 1 |
    9. | snake | 1 |
    10. +---------+----------+

    每种性别的动物数量:

    1. mysql> SELECT sex, COUNT(*) FROM pet GROUP BY sex;
    2. +------+----------+
    3. | sex | COUNT(*) |
    4. +------+----------+
    5. | NULL | 1 |
    6. | f | 4 |
    7. | m | 4 |
    8. +------+----------+

    (在这个输出结果中,NULL表示性别未知)

    每个物种各性别对应的动物数量:

    1. mysql> SELECT species, sex, COUNT(*) FROM pet GROUP BY species, sex;
    2. +---------+------+----------+
    3. | species | sex | COUNT(*) |
    4. +---------+------+----------+
    5. | bird | NULL | 1 |
    6. | bird | f | 1 |
    7. | cat | f | 1 |
    8. | cat | m | 1 |
    9. | dog | f | 1 |
    10. | dog | m | 2 |
    11. | hamster | f | 1 |
    12. | snake | m | 1 |
    13. +---------+------+----------+

    当你在使用时COUNT()时,你不需要检索整个表。例如,对于前面的查询,当只查询dogs和cats时,可以像这样:

    1. mysql> SELECT species, sex, COUNT(*) FROM pet
    2. -> WHERE species = 'dog' OR species = 'cat'
    3. -> GROUP BY species, sex;
    4. +---------+------+----------+
    5. | species | sex | COUNT(*) |
    6. +---------+------+----------+
    7. | cat | f | 1 |
    8. | cat | m | 1 |
    9. | dog | f | 1 |
    10. | dog | m | 2 |
    11. +---------+------+----------+

    或者,如果你想知道,sex是已知的那些动物,每个性别所拥有的动物数量:

    1. mysql> SELECT species, sex, COUNT(*) FROM pet
    2. -> WHERE sex IS NOT NULL
    3. -> GROUP BY species, sex;
    4. +---------+------+----------+
    5. | species | sex | COUNT(*) |
    6. +---------+------+----------+
    7. | bird | f | 1 |
    8. | cat | f | 1 |
    9. | cat | m | 1 |
    10. | dog | f | 1 |
    11. | dog | m | 2 |
    12. | hamster | f | 1 |
    13. | snake | m | 1 |
    14. +---------+------+----------+

    如果你的列名被用于COUNT()来求值,一个GROUP BY谓詞应该也被用于这些列名,否则,会有如下错误:

    如果ONLY_FULL_GROUP_BY模式是启用的,会发生这样的错误:

    1. mysql> SET sql_mode = 'ONLY_FULL_GROUP_BY';
    2. Query OK, 0 rows affected (0.00 sec)
    3. mysql> SELECT owner, COUNT(*) FROM pet;
    4. ERROR 1140 (42000): In aggregated query without GROUP BY, expression
    5. #1 of SELECT list contains nonaggregated column 'menagerie.pet.owner';
    6. this is incompatible with sql_mode=only_full_group_by

    如果ONLY_FULL_GROUP_BY没有启用,这个查询会把所有的行都当成一个单一的组来处理,但是,被选择出的列名的值是不确定的。服务器是自动选择了任意行的值:

    1. mysql> SET sql_mode = '';
    2. Query OK, 0 rows affected (0.00 sec)
    3. mysql> SELECT owner, COUNT(*) FROM pet;
    4. +--------+----------+
    5. | owner | COUNT(*) |
    6. +--------+----------+
    7. | Harold | 8 |
    8. +--------+----------+
    9. 1 row in set (0.00 sec)

    See also Section 13.20.3, “MySQL Handling of GROUP BY”.

    原文: https://strongyoung.gitbooks.io/mysql-reference-manual/content/tutorial/creating_using_database/counting_rows.html