MySQL Order by

The ORDER BY keyword is used to sort the data in a recordset.
The ORDER BY keyword sort the records in ascending order by default.
If you want to sort the records in a descending order, you can use the DESC keyword.
Syntax:
1
2
3
4
5
SELECT column_name(s)
 
FROM table_name
 
ORDER BY column_name(s) ASC|DESC
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("my_db", $con);
 
mysql_query("UPDATE Persons SET Age = '36'
WHERE FirstName = 'Peter' AND LastName = 'Griffin'");
 
mysql_close($con);
?>
The output of the code above will be:

Glenn Quagmire 33

Peter Griffin 35 

Order by Two Columns

It is also possible to order by more than one column. When ordering by more than one column, the second column is only used if the values in the first column are equal:
1
2
3
4
5
SELECT column_name(s)
 
FROM table_name
 
ORDER BY column1, column2
MySQL Where
MySQL Order By
MySQL Delete
XML Expat Parser