Oracle 5.0 Reference Manual page 231

Table of Contents

Advertisement

| 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,
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 |
| bird
| f
|
| cat
| f
|
| cat
| m
|
| dog
| f
|
| dog
| m
|
| hamster | f
|
| snake
| m
|
+---------+------+----------+
You need not retrieve an entire table when you use
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
|
| cat
| m
|
| dog
| f
|
| dog
| m
|
+---------+------+----------+
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
|
| cat
| f
|
| cat
| m
|
| dog
| f
|
| dog
| m
|
| hamster | f
|
| snake
| m
|
+---------+------+----------+
If you name columns to select in addition to the
present that names those same columns. Otherwise, the following occurs:
• If the
ONLY_FULL_GROUP_BY
mysql>
SET sql_mode = 'ONLY_FULL_GROUP_BY';
Retrieving Information from a Table
1 |
1 |
1 |
1 |
1 |
2 |
1 |
1 |
1 |
1 |
1 |
2 |
1 |
1 |
1 |
1 |
2 |
1 |
1 |
COUNT()
[538]
SQL mode is enabled, an error occurs:
211
[970]. For example, the previous query,
COUNT()
[970]
value, a
GROUP BY
clause should be

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents