php mysql order by 关键词对php有着重要的作用,本篇将详细的讲解php mysql order by 关键词的相关知识。
order by 关键词
order by 关键词用于对记录集中的数据进行排序。
order by 关键词默认对记录进行升序排序。
如果你想降序排序,请使用 desc 关键字。
语法
select column_name(s)from table_name
order by column_name(s) asc|desc
如需学习更多关于 sql 的知识,请访问我们的 sql 教程。
实例
下面的实例选取 persons 表中存储的所有数据,并根据 age 列对结果进行排序:
<?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);?>
以上结果将输出:
glenn quagmire 33peter griffin 35
根据两列进行排序
可以根据多个列进行排序。当按照多个列进行排序时,只有第一列的值相同时才使用第二列:
select column_name(s)from table_name
order by column1, column2
本篇详细的讲解了php mysql order by 关键词的相关内容,更多的学习资料清关注即可观看。
相关推荐:
掌握php mysql where 子句
怎样通过php mysql 读取数据
关于php mysql 预处理语句的相关知识
以上就是关于php mysql order by 关键词的相关知识的详细内容。