Changes

Sorting Data Retrieved from a MySQL Database

1,357 bytes added, 20:18, 11 October 2007
Sorting on Multiple Columns
10 rows in set (0.00 sec)
</pre>
 
== Sorting Data in Descending Order ==
 
So far in this chapter we have sorted all our MySQL database queries in ascending order. Suppose, however, that visitor to your web site needs to view the prices for a list of items starting with the most expensive. Clearly the standard ''ORDER BY'' specification will not address this need. Fortunately SQL includes the ''DESC'' keyword to specify that the results of a ''SELECT'' statement are to be sorted in descending order. For example, to sort the product names in descending order:
 
<pre>
mysql> SELECT prod_name FROM product ORDER BY prod_name DESC;
+----------------------------+
| prod_name |
+----------------------------+
| WildTech 250Gb 1700 |
| Moto Razr |
| Microsoft 10-20 Keyboard |
| Kensington Ci20 |
| EasyTech Mouse 7632 |
| Dell XPS 400 |
| CD-RW Model 4543 |
| Buffalo AirStation Turbo G |
| Apple iPod Touch |
| Apple iPhone 8Gb |
+----------------------------+
10 rows in set (0.00 sec)
</pre>
 
== Summary ==
 
In this chapter we have looked at how to use the ''SELECT'' statement in conjunction with the ''ORDER BY'' and ''DESC'' keywords to retrieve and sort data from a MySQL database table. In the next chapter we explore ways to filter retrieved data using the ''WHERE'' keyword.