Home  >  Article  >  Backend Development  >  Related knowledge about PHP MySQL Order By keyword

Related knowledge about PHP MySQL Order By keyword

jacklove
jackloveOriginal
2018-05-07 14:21:211813browse

PHP MySQL Order By keyword plays an important role in php. This article will explain the relevant knowledge of PHP MySQL Order By keyword in detail.

ORDER BY keyword

The ORDER BY keyword is used to sort the data in the 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 following example selects all the data stored in the "Persons" table and sorts the results according to the "Age" column:

<?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[&#39;FirstName&#39;];    echo " " . $row[&#39;LastName&#39;];    echo " " . $row[&#39;Age&#39;];    echo "<br>";}mysqli_close($con);?>

The above results will be output:

Glenn Quagmire 33Peter Griffin 35

Sort based on two columns

You can sort based on multiple columns. When sorting by multiple columns, the second column is used only if the value of the first column is the same:

SELECT column_name(s)FROM table_name
ORDER BY column1, column2

This article explains in detail the relevant content of the PHP MySQL Order By keyword. For more learning materials, please pay attention to the PHP Chinese website.

Related recommendations:

Master the PHP MySQL Where clause

How to read data through PHP MySQL

Related knowledge about PHP MySQL prepared statements

The above is the detailed content of Related knowledge about PHP MySQL Order By keyword. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn