Database Reference
In-Depth Information
Figure 10.4
Counting groups with GROUP BY.
Figure 10.5 shows the results. Notice that on this occasion we have not grouped by the
clause that we are counting, but that does not matter, the count is really just counting rows,
not columns. So the query is saying: “Give me the number rows of the table that refer to
each specific webpage”.
Now this is getting powerful; the simple query has shown that our Home page has had
three hits, and our visitorbook and links page have each received one hit. Imagine how use-
ful this data would be for a table that contained tens of thousands of hits on hundreds of
webpages. If we were performing such a query and wanted to restrict the results to the first
10 pages, then we would add a WHERE clause as follows:
SELECT id, count(*)
FROM log
WHERE webpageid <=10
GROUP BY webpageid
Notice the position of the WHERE clause. If you put it after the GROUP BY you get an
error message.
Figure 10.5
Counting groups with GROUP BY.
Search WWH ::




Custom Search