PHP MySQL Order By 关键词
<div id="article_content" class="article_content clearfix"><link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
<link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-6e43165c0a.css">
<div id="content_views" class="htmledit_views">
<h2>ORDER BY 关键词</h2>
<p>ORDER BY 关键词用于对记录集中的数据进行排序。</p>
<p>ORDER BY 关键词默认对记录进行升序排序。</p>
<p>如果你想降序排序,请使用 DESC 关键字。</p>
<h3>语法</h3>
<pre>SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC</pre>
<p>如需学习更多关于 SQL 的知识,请访问我们的 <ahref="https://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a>。</p>
<h3>实例</h3>
<p>下面的实例选取 "Persons" 表中存储的所有数据,并根据 "Age" 列对结果进行排序:</p>
<pre><?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);
?></pre>
<p>以上结果将输出:</p>
<pre>Glenn Quagmire 33
Peter Griffin 35</pre>
<p></p>
<hr>
<h2>根据两列进行排序</h2>
<p>可以根据多个列进行排序。当按照多个列进行排序时,只有第一列的值相同时才使用第二列:</p>
<pre>SELECT column_name(s)
FROM table_name
ORDER BY column1, column2</pre>
</div>
</div>
<div id="treeSkill"></div>
页:
[1]