Changes

Jump to: navigation, search

Sorting Data Retrieved from a MySQL Database

1,613 bytes added, 20:07, 11 October 2007
Sorting on Multiple Columns
== Sorting on Multiple Columns ==
 
In the previous examples we have used a single table column as the criteria for sorting data retrieved from a MySQL database. ''ORDER BY'' also provides a way to sort on multiple columns. The columns to be used for the sort are specified after the ''ORDER BY'' keywords and separated by commas. For example, to sort on a combination of the product name and product description in our table we would execute the following SQL statement:
 
<pre>
SELECT * FROM product ORDER BY prod_name, prod_desc;
</pre>
 
which results in the following data ordering:
 
<pre>
+-----------+----------------------------+-----------------------------+
| prod_code | prod_name | prod_desc |
+-----------+----------------------------+-----------------------------+
| 5 | Apple iPhone 8Gb | Smart Phone |
| 8 | Apple iPod Touch | Portable Music/Movie Player |
| 10 | Buffalo AirStation Turbo G | Wireless Ethernet Bridge |
| 1 | CD-RW Model 4543 | CD Writer |
| 7 | Dell XPS 400 | Desktop PC |
| 2 | EasyTech Mouse 7632 | Cordless Mouse |
| 9 | Kensington Ci20 | Optical Mouse |
| 4 | Microsoft 10-20 Keyboard | Ergonmoc Keyboard |
| 6 | Moto Razr | Mobile Phone |
| 3 | WildTech 250Gb 1700 | SATA Disk Drive |
+-----------+----------------------------+-----------------------------+
10 rows in set (0.00 sec)
</pre>

Navigation menu