Rechercher dans le manuel MySQL
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:
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:
- +--------+----------+
- +--------+----------+
- | Benny | 2 |
- | Diane | 2 |
- | Gwen | 3 |
- | Harold | 2 |
- +--------+----------+
The preceding query uses GROUP BY
to group
all records for each owner
. The use of
COUNT()
in conjunction with
GROUP BY
is useful for characterizing your
data under various groupings. The following examples show
different ways to perform animal census operations.
Number of animals per species:
- +---------+----------+
- +---------+----------+
- | bird | 2 |
- | cat | 2 |
- | dog | 3 |
- | hamster | 1 |
- | snake | 1 |
- +---------+----------+
Number of animals per sex:
- +------+----------+
- +------+----------+
- | f | 4 |
- | m | 4 |
- +------+----------+
(In this output, NULL
indicates that the
sex is unknown.)
Number of animals per combination of species and sex:
- +---------+------+----------+
- +---------+------+----------+
- | 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()
. For example, the
previous query, when performed just on dogs and cats, looks
like this:
- +---------+------+----------+
- +---------+------+----------+
- | 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:
- +---------+------+----------+
- +---------+------+----------+
- | 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. Otherwise, the following occurs:
If the
ONLY_FULL_GROUP_BY
SQL mode is enabled, an error occurs:- Query OK, 0 rows affected (0.00 sec)
- #1 of SELECT list contains nonaggregated column 'menagerie.pet.owner';
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. The server is free to select the value from any row:
See also Section 12.20.3, “MySQL Handling of GROUP BY”. See
Section 12.20.1, “Aggregate (GROUP BY) Function Descriptions” for information about
COUNT(
behavior and related optimizations.
expr
)
Traduction non disponible
Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-counting-rows.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.