PHP MySQL Order By Keyword
ORDER BY keyword is used to sort the record set data.
ORDER BY Keyword
ORDER BY keyword is used to sort the record set data.
ORDER BY keyword default records in ascending order.
If you want to sort in descending order, please use the DESC keyword.
grammar
SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC|DESC
To learn more about SQL knowledge, please visit our SQL tutorial .
Examples
The following examples select all data "Persons" stored in the table, and according to "Age" column to sort the results:
<?php $con=mysqli_connect("localhost","username","password","database"); // 检测连接 if (mysqli_connect_errno()) { echo "连接失败: " . mysqli_connect_error(); } $result = mysqli_query($con,"SELECT * FROM Persons ORDER BY age"); while($row = mysqli_fetch_array($result)) { echo $row['FirstName']; echo " " . $row['LastName']; echo " " . $row['Age']; echo "<br>"; } mysqli_close($con); ?>
These results will output:
Glenn Quagmire 33 Peter Griffin 35
Sorted according to two
You can be sorted according to multiple columns. When sorting by multiple columns, only the first column value when using the same in the second column:
SELECT column_name(s) FROM table_name ORDER BY column1, column2