1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Ordering MySQL Data

By

Man working on a laptop
Forest Woodward/E+/Getty Images

How do I order MySQL data?:

When you query your MySQL database, you can sort the results by any field in an ascending or descending order by just adding 'ORDER BY' at the end of your query. You would use ORDER BY field_name ASC for an ascending sort, or ORDER BY field_name DESC for a descending sort.

 $data = mysql_query("SELECT * FROM address ORDER BY name ASC") 
In the code above we are retrieving data from an address book, and we are sorting by the person's name in an ascending fashion.
 $data = mysql_query("SELECT email FROM address ORDER BY email DESC") 
Here we are selecting only the email addresses, and listing them in a descending order.

 

  1. About.com
  2. Technology
  3. PHP / MySQL
  4. Learn MySQL
  5. Ordering MySQL Data

©2014 About.com. All rights reserved.