Home > php教程 > php手册 > PHP MySQL Order By,phpmysqlorderby

PHP MySQL Order By,phpmysqlorderby

WBOY
Release: 2016-06-13 08:38:47
Original
1038 people have browsed it

PHP MySQL Order By,phpmysqlorderby

ORDER BY 关键词用于对记录集中的数据进行排序。

ORDER BY 关键词

ORDER BY 关键词用于对记录集中的数据进行排序。

ORDER BY 关键词默认对记录进行升序排序。

如果你想降序排序,请使用 DESC 关键字。

语法

SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC
Copy after login

实例

下面的实例选取 "Persons" 表中存储的所有数据,并根据 "Age" 列对结果进行排序:

<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . 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);
?>
Copy after login

以上结果将输出:

<code class="hljs nginx"><span class="hljs-title">Glenn Quagmire <span class="hljs-number">33
Peter Griffin <span class="hljs-number">35</span></span></span></code>
Copy after login

根据两列进行排序

可以根据多个列进行排序。当按照多个列进行排序时,只有第一列的值相同时才使用第二列:

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

 

相关阅读:

php 编程笔记分享 - 非常实用

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template