Keine Cache-Version


Caching deaktiviert Standardeinstellung für diese Seite:aktiviert (code DEF204)
Wenn die Anzeige zu langsam ist, können Sie den Benutzermodus deaktivieren, um die zwischengespeicherte Version anzuzeigen.

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:

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

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:

  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. +--------+----------+

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:

  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. +---------+----------+

Number of animals per sex:

  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. +------+----------+

(In this output, NULL indicates that the sex is unknown.)

Number of animals per combination of species and sex:

  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. +---------+------+----------+

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:

  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. +---------+------+----------+

Or, if you wanted the number of animals per sex only for animals whose sex is known:

  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. +---------+------+----------+

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:

    1. mysql> SET sql_mode = 'ONLY_FULL_GROUP_BY';
    2. Query OK, 0 rows affected (0.00 sec)
    3.  
    4. mysql> SELECT owner, COUNT(*) FROM pet;
    5. ERROR 1140 (42000): In aggregated query without GROUP BY, expression
    6. #1 of SELECT list contains nonaggregated column 'menagerie.pet.owner';
    7. 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. The server is free to select the value from any row:

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

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(expr) behavior and related optimizations.


Suchen Sie im MySQL-Handbuch

Deutsche Übersetzung

Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.

Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.

Vielen Dank im Voraus.

Dokument erstellt 26/06/2006, zuletzt geändert 26/10/2018
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/mysql-rf-counting-rows.html

Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.

Referenzen

  1. Zeigen Sie - html-Dokument Sprache des Dokuments:en Manuel MySQL : https://dev.mysql.com/

Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor Diese Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.

Inhaltsverzeichnis Haut