Home > Database > Mysql Tutorial > MySQL 教程:Order By语法_MySQL

MySQL 教程:Order By语法_MySQL

WBOY
Release: 2016-06-01 13:57:22
Original
1152 people have browsed it

   相关文章:MySQL教程:Order By用法

MySQL Order By keyword是用来给记录中的数据进行分类的。

  MySQL Order By Keyword根据关键词分类

  ORDER BY keyword是用来给记录中的数据进行分类的。

  MySQL Order By语法

SELECT column_name(s)
FROM table_name
ORDER BY column_name

  注意:SQL语句是“字母大小写不敏感”的语句(它不区分字母的大小写),即:“ORDER BY”和“order by”是一样的。

  MySQL Order By案例

  下面的例子:从“Person”表中选取所有记录,并将“Age”列进行分类:

$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 "
";
  }
mysql_close($con);
?>

  上面的代码将输出下面的结果:

  Glenn Quagmire 33

  Peter Griffin 35

  按照升序或者降序进行分类排列

  如果你使用了“ORDER BY”关键词,所有记录将按照默认的升序进行排列(即:从1到9,从a到z)

  使用“DESC”关键词可以制定所有的数据按照降序排列(即:从9到1,从z到a):

SELECT column_name(s)
FROM table_name
ORDER BY column_name DESC

  MySQL Order By根据两列进行分类

  很多时候,我们需要同时根据两列内容(或者更多列)来对数据进行分类。当指定的列数多于一列时,仅在第一列的值完全相同时才参考第二列:

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

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template