Database Reference
In-Depth Information
| TNT (1 stick) |
| TNT (5 sticks) |
+----------------+
Tip
Sorting by Nonselected Columns More often than not, the columns used in an O RDER
BY clause are ones that were selected for display. However, this is actually not required,
and it is perfectly legal to sort data by a column that is not retrieved.
Sorting by Multiple Columns
It is often necessary to sort data by more than one column. For example, if
you are displaying an employee list, you might want to display it sorted by last
name and first name (first sort by last name, and then within each last name sort
by first name). This would be useful if there are multiple employees with the
same last name.
To sort by multiple columns, simply specify the column names separated by
commas (just as you do when you are selecting multiple columns).
The following code retrieves three columns and sorts the results by two of
them—first by price and then by name.
Input
SELECT prod_id, prod_price, prod_name
FROM products
ORDER BY prod_price, prod_name;
Output
+---------+------------+----------------+
| prod_id | prod_price | prod_name |
+---------+------------+----------------+
| FC | 2.50 | Carrots |
| TNT1 | 2.50 | TNT (1 stick) |
| FU1 | 3.42 | Fuses |
| SLING | 4.49 | Sling |
| ANV01 | 5.99 | .5 ton anvil |
| OL1 | 8.99 | Oil can |
| ANV02 | 9.99 | 1 ton anvil |
| FB | 10.00 | Bird seed |
| TNT2 | 10.00 | TNT (5 sticks) |
| DTNTR | 13.00 | Detonator |
| ANV03 | 14.99 | 2 ton anvil |
 
 
Search WWH ::




Custom Search