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

PHP
WINDOWS下安装MySQL
PHP 制作 网站/服务器 监视脚本
用PHP和CSS制作活动按钮
PHP 单件模式
PHP MVC模式,类封装以及HACK
PHP 中使用正则表达式
PHP 防止 SQL 注入攻击
PHP 跨站点脚本攻击
PHP 防止用户操纵 GET 变量
PHP 防止远程表单提交

PHP 中的 MySQL Order By


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 871 ::
收藏到网摘: 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

登陆 | 还没注册?