Grouping Selected Results

In the preceding chapter, two different clauses—ORDER BY and LIMIT—were introduced as ways of affecting the returned results. The former dictates the order in which the selected rows are returned; the latter dictates which of the selected rows are actually returned. This next clause, GROUP BY, is different in that it works by grouping the returned data into similar blocks of information. For example, to group all of the URLs by category, you would use

SELECT * FROM url_associations, urls
→ WHERE url_associations.url_id=urls.
→ url_id GROUP BY url_category_id

The returned data is altered in that you’ve now aggregated the information instead of returned just the specific itemized records. So where you might have seven ...

Get PHP and MySQL for Dynamic Web Sites: Visual Quickpro Guide, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.