Piège
COUNT(*) ou COUNT(colonne)
COUNT(colonne) ignore les NULL ; COUNT(*) compte les lignes.Pour compter des lignes, COUNT(*). Réservez COUNT(colonne) à « les lignes où cette colonne est
remplie », et COUNT(DISTINCT colonne) aux valeurs distinctes.
SELECT COUNT(*) AS all_rows,
COUNT(city) AS with_city,
COUNT(DISTINCT city) AS cities
FROM customers;
| all_rows | with_city | cities |
|---|---|---|
| 400 | 382 | 27 |
Pratiquer :COUNT(*) ou COUNT(city) ?