Recipe 12.11. Using a Join to Control Query Output Order


Problem

You want to sort a statement's output using a characteristic of the output that cannot be specified using ORDER BY . For example, you want to sort a set of rows by subgroups, putting first those groups with the most rows and last those groups with the fewest rows. But "number of rows in each group" is not a property of individual rows, so you can't use it for sorting.

Solution

Derive the ordering information and store it in an auxiliary table. Then join the original table to the auxiliary table, using the auxiliary table to control the sort order.

Discussion

Most of the time when you sort a query result, you use an ORDER BY clause that names which column or columns to use for sorting. But sometimes the values you want to sort by aren't present in the rows to be sorted. This is the case when you want to use group characteristics to order the rows. The following example uses the rows in the driver_log table to illustrate this. The table contains these rows:

mysql> SELECT * FROM driver_log ORDER BY rec_id; +--------+-------+------------+-------+ | rec_id | name  | trav_date  | miles | +--------+-------+------------+-------+ |      1 | Ben   | 2006-08-30 |   152 | |      2 | Suzi  | 2006-08-29 |   391 | |      3 | Henry | 2006-08-29 |   300 | |      4 | Henry | 2006-08-27 |    96 | |      5 | Ben   | 2006-08-29 |   131 | |      6 | Henry | 2006-08-26 |   115 | |      7 | Suzi  | 2006-09-02 |   502 | |      8 | Henry | 2006-09-01 |   197 | |      9 | Ben   | 2006-09-02 |    79 | |     10 | Henry | 2006-08-30 |   203 | +--------+-------+------------+-------+ 

The preceding statement sorts the rows using the ID column, which is present in the rows. But what if you want to display a list and sort it on the basis of a summary value not present in the rows? That's a little trickier. Suppose that you want to show each driver's rows by date, but place those drivers who drive the most miles first. You can't do this with a summary query, because then you wouldn't get back the individual driver rows. But you can't do it without a summary query, either, because the summary values are required for sorting. The way out of the dilemma is to create another table containing the summary value per driver and then join it to the original table. That way you can produce the individual rows and also sort them by the summary values.

To summarize the driver totals into another table, do this:

mysql> CREATE TABLE tmp     -> SELECT name, SUM(miles) AS driver_miles FROM driver_log GROUP BY name;             

This produces the values we need to put the names in the proper total-miles order:

mysql> SELECT * FROM tmp ORDER BY driver_miles DESC; +-------+--------------+ | name  | driver_miles | +-------+--------------+ | Henry |          911 | | Suzi  |          893 | | Ben   |          362 | +-------+--------------+ 

Then use the name values to join the summary table to the driver_log table, and use the driver_miles values to sort the result. The following statement shows the mileage totals in the result. That's only to clarify how the values are being sorted. It's not actually necessary to display them; they're needed only for the ORDER BY clause.

mysql> SELECT tmp.driver_miles, driver_log.*     -> FROM driver_log INNER JOIN tmp     -> ON driver_log.name = tmp.name     -> ORDER BY tmp.driver_miles DESC, driver_log.trav_date; +--------------+--------+-------+------------+-------+ | driver_miles | rec_id | name  | trav_date  | miles | +--------------+--------+-------+------------+-------+ |          911 |      6 | Henry | 2006-08-26 |   115 | |          911 |      4 | Henry | 2006-08-27 |    96 | |          911 |      3 | Henry | 2006-08-29 |   300 | |          911 |     10 | Henry | 2006-08-30 |   203 | |          911 |      8 | Henry | 2006-09-01 |   197 | |          893 |      2 | Suzi  | 2006-08-29 |   391 | |          893 |      7 | Suzi  | 2006-09-02 |   502 | |          362 |      5 | Ben   | 2006-08-29 |   131 | |          362 |      1 | Ben   | 2006-08-30 |   152 | |          362 |      9 | Ben   | 2006-09-02 |    79 | +--------------+--------+-------+------------+-------+ 

To avoid using the temporary table, select the same rows using a subquery in the FROM clause:

mysql> SELECT tmp.driver_miles, driver_log.*     -> FROM driver_log INNER JOIN     -> (SELECT name, SUM(miles) AS driver_miles     -> FROM driver_log GROUP BY name) AS tmp     -> ON driver_log.name = tmp.name     -> ORDER BY tmp.driver_miles DESC, driver_log.trav_date; +--------------+--------+-------+------------+-------+ | driver_miles | rec_id | name  | trav_date  | miles | +--------------+--------+-------+------------+-------+ |          911 |      6 | Henry | 2006-08-26 |   115 | |          911 |      4 | Henry | 2006-08-27 |    96 | |          911 |      3 | Henry | 2006-08-29 |   300 | |          911 |     10 | Henry | 2006-08-30 |   203 | |          911 |      8 | Henry | 2006-09-01 |   197 | |          893 |      2 | Suzi  | 2006-08-29 |   391 | |          893 |      7 | Suzi  | 2006-09-02 |   502 | |          362 |      5 | Ben   | 2006-08-29 |   131 | |          362 |      1 | Ben   | 2006-08-30 |   152 | |          362 |      9 | Ben   | 2006-09-02 |    79 | +--------------+--------+-------+------------+-------+ 




MySQL Cookbook
MySQL Cookbook
ISBN: 059652708X
EAN: 2147483647
Year: 2004
Pages: 375
Authors: Paul DuBois

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net