1. Home
  2. Computing & Technology
  3. PHP / MySQL

Ordering MySQL Data

By Angela Bradley, About.com

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.
Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Learn MySQL
  5. SQL Order By - Order By ASC - Order By DESC

©2009 About.com, a part of The New York Times Company.

All rights reserved.