当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > MySQL Order By

PHP
PHP 介绍
PHP 安装
PHP 语法
PHP 变量
PHP操作符
PHP If...Else
PHP Switch
PHP 数组
PHP 循环
PHP 函数
PHP 表单
PHP $_GET
PHP $_POST
PHP Date
PHP Include
PHP 文件处理
PHP 文件上传
PHP Cookies
PHP Sessions
PHP 发送邮件

PHP 中的 MySQL Order By


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 916 ::
收藏到网摘: n/a

The ORDER BY keyword is used to sort the data in a recordset.
ORDER BY keyword是用来给记录中的数据进行分类的。


The ORDER BY Keyword
PHP MySQL Order By Keyword[根据关键词分类]

The ORDER BY keyword is used to sort the data in a recordset.
ORDER BY keyword是用来给记录中的数据进行分类的。

Syntax
语法

SELECT column_name(s)
FROM table_name
ORDER BY column_name

Note: SQL statements are not case sensitive. ORDER BY is the same as order by.
注意:SQL语句是“字母大小写不敏感”的语句(它不区分字母的大小写),即:“ORDER BY”和“order by”是一样的。

Example
案例

The following example selects all the data stored in the "Person" table, and sorts the result by the "Age" column:
下面的例子:从“Person”表中选取所有记录,并将“Age”列进行分类:

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM person ORDER BY age");
while($row = mysql_fetch_array($result)) { echo $row['FirstName'] echo " " . $row['LastName']; echo " " . $row['Age']; echo "<br />"; }
mysql_close($con);
?>

The output of the code above will be:
上面的代码将输出下面的结果:

Glenn Quagmire 33
Peter Griffin 35


Sort Ascending or Descending
按照升序[Ascending]或者降序[Descending]进行分类排列

If you use the ORDER BY keyword, the sort-order of the recordset is ascending by default (1 before 9 and "a" before "p").
如果你使用了“ORDER BY”关键词,所有记录将按照默认的升序进行排列(即:从1到9,从a到z)

Use the DESC keyword to specify a descending sort-order (9 before 1 and "p" before "a"):
使用“DESC”关键词可以制定所有的数据按照降序排列(即:从9到1,从z到a):

SELECT column_name(s)
FROM table_name
ORDER BY column_name DESC


Order by Two Columns
根据两列进行分类

It is possible to order by more than one column. When ordering by more than one column, the second column is only used if the values in the first column are identical:
很多时候,我们需要同时根据两列内容(或者更多列)来对数据进行分类。当指定的列数多于一列时,仅在第一列的值完全相同时才参考第二列:

SELECT column_name(s)
FROM table_name
ORDER BY column_name1, column_name2

评论 (0) All

登陆 | 还没注册?