Database Reference
In-Depth Information
Aggregate Functions 10
Count
Aggregate functions in MySQL allow you to calculate a summary of values from a column
and from groups of values within a column. As usual, the best way to demonstrate this con-
cept is with an example.
Count in MySQL is a function that allows you to count the number of specified items in
a table. One way that you can use count is as follows:
SELECT count(columnname)
FROM
tablename
We can use the above statement to count the number of rows within a table by using the
following script:
SELECT count(*)
FROM
visitorbook
If you execute that query you will get the very simple results shown in Figure 10.1.
You may wonder why you would bother performing a query like this when you can just
look at the top row of the output to tell you how many rows came from your query.
However, when you are accessing these functions from within a script language, like PHP,
then it is more useful. For instance, the following script counts all of the hits in our log
table, thus producing a counter for the whole of the website:
Figure 10.1
Counting rows in a table.
107
Search WWH ::




Custom Search