Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP MySQL Order By Keyword

PHP MySQL Order By Keyword


The ORDER BY keyword is used to sort the data in a record set.


ORDER BY Keyword

The ORDER BY keyword is used to sort the data in a record set.

The ORDER BY keyword sorts records in ascending order by default.

If you want to sort in descending order, use the DESC keyword.

Syntax



SELECT column_name(s)

FROM table_name

ORDER BY column_name(s) ASC|DESC

To learn more about SQL, please visit our SQL tutorial.

Example

The example below selects all data stored in the "Persons" table and sorts the results based on the "Age" column:



";

}



mysqli_close($con);

?>

The above result will output:



Glenn Quagmire 33

Peter Griffin 35


Sort by Two Columns

You can sort by multiple columns. When sorting by multiple columns, the second column is used only when the values in the first column are the same:



SELECT column_name(s)

FROM table_name

ORDER BY column1, column2