Oracle 5.0 Reference Manual page 230

Table of Contents

Advertisement

To find names containing exactly five characters, use "^" and "$" to match the beginning and end of the
name, and five instances of "." in between:
mysql>
SELECT * FROM pet WHERE name REGEXP '^.....$';
+-------+--------+---------+------+------------+-------+
| name
| owner
+-------+--------+---------+------+------------+-------+
| Claws | Gwen
| Buffy | Harold | dog
+-------+--------+---------+------+------------+-------+
You could also write the previous query using the
mysql>
SELECT * FROM pet WHERE name REGEXP '^.{5}$';
+-------+--------+---------+------+------------+-------+
| name
| owner
+-------+--------+---------+------+------------+-------+
| Claws | Gwen
| Buffy | Harold | dog
+-------+--------+---------+------+------------+-------+
Section 12.5.2, "Regular
expressions.
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
table?" because there is one record per pet.
pet
query to count your animals looks like this:
mysql>
SELECT COUNT(*) FROM pet;
+----------+
| COUNT(*) |
+----------+
|
9 |
+----------+
Earlier, you retrieved the names of the people who owned pets. You can use
want to find out how many pets each owner has:
mysql>
SELECT owner, COUNT(*) FROM pet GROUP BY owner;
+--------+----------+
| owner
| COUNT(*) |
+--------+----------+
| Benny
|
| Diane
|
| Gwen
|
| Harold |
+--------+----------+
The preceding query uses
[970]
COUNT()
groupings. The following examples show different ways to perform animal census operations.
Number of animals per species:
mysql>
SELECT species, COUNT(*) FROM pet GROUP BY species;
+---------+----------+
| species | COUNT(*) |
+---------+----------+
| bird
|
| cat
|
| dog
|
| hamster |
Retrieving Information from a Table
| species | sex
| birth
| cat
| m
| 1994-03-17 | NULL
| f
| 1989-05-13 | NULL
| species | sex
| birth
| cat
| m
| 1994-03-17 | NULL
| f
| 1989-05-13 | NULL
Expressions", provides more information about the syntax for regular
2 |
2 |
3 |
2 |
GROUP BY
in conjunction with
GROUP BY
2 |
2 |
3 |
1 |
| death |
|
|
("repeat-n-times") operator:
{n}
| death |
|
|
[970]
COUNT(*)
to group all records for each owner. The use of
is useful for characterizing your data under various
210
counts the number of rows, so the
[970]
COUNT()
if you

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents