w3pop.com :: 网络学院 :: PHP :: MySQL Order By
The ORDER BY keyword is used to sort the data in a recordset.
ORDER BY keyword是用来给记录中的数据进行分类的。
The ORDER BY keyword is used to sort the data in a recordset.
ORDER BY keyword是用来给记录中的数据进行分类的。
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”是一样的。
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 |
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 |
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