Using a Join to Control Query Output Order
12.15.1 Problem
You want to sort a query'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 sort by it.
12.15.2 Solution
Derive the ordering information and store it in another table. Then join the original table to the derived table, using the derived table to control the sort order.
12.15.3 Discussion
Most of the time when you sort a query result, you use an ORDER BY (or GROUP BY) clause to name the 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, for example, if you want to use group characteristics to order the rows. The following example uses the records in the driver_log table to illustrate this. The table looks like this:
mysql> SELECT * FROM driver_log ORDER BY id; +--------+-------+------------+-------+ | rec_id | name | trav_date | miles | +--------+-------+------------+-------+ | 1 | Ben | 2001-11-30 | 152 | | 2 | Suzi | 2001-11-29 | 391 | | 3 | Henry | 2001-11-29 | 300 | | 4 | Henry | 2001-11-27 | 96 | | 5 | Ben | 2001-11-29 | 131 | | 6 | Henry | 2001-11-26 | 115 | | 7 | Suzi | 2001-12-02 | 502 | | 8 | Henry | 2001-12-01 | 197 | | 9 | Ben | 2001-12-02 | 79 | | 10 | Henry | 2001-11-30 | 203 | +--------+-------+------------+-------+
The preceding query sorts the records 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 you want to show each driver's records 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 records. 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 values, then join it to the original table. That way you can produce the individual records, 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;
That produces the values we need to put the names in the proper 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 query below shows the mileage totals in the result. That's only to make it clearer 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, tmp
-> WHERE 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 | 2001-11-26 | 115 |
| 911 | 4 | Henry | 2001-11-27 | 96 |
| 911 | 3 | Henry | 2001-11-29 | 300 |
| 911 | 10 | Henry | 2001-11-30 | 203 |
| 911 | 8 | Henry | 2001-12-01 | 197 |
| 893 | 2 | Suzi | 2001-11-29 | 391 |
| 893 | 7 | Suzi | 2001-12-02 | 502 |
| 362 | 5 | Ben | 2001-11-29 | 131 |
| 362 | 1 | Ben | 2001-11-30 | 152 |
| 362 | 9 | Ben | 2001-12-02 | 79 |
+--------------+--------+-------+------------+-------+
Категории