ORDER BY 语句

ORDER BY 语句用于根据指定的列对结果集进行排序。

ORDER BY 语句默认按照升序对记录进行排序。

如果您希望按照降序对记录进行排序,可以使用 DESC 关键字。

1.以时间显示,升序

mysql> select * from Orders order by OrderDate;
+------+------------+------------+----------+
| O_Id | OrderDate  | OrderPrice | Customer |
+------+------------+------------+----------+
|    1 | 2008-07-22 |    1000.00 | Carter   |
|    6 | 2008-11-22 |    1000.00 | Adams    |
|    5 | 2008-11-29 |    1000.00 | Bush     |
|    2 | 2008-12-24 |    1000.00 | Bush     |
|    3 | 2008-12-26 |    1000.00 | tom      |
|    4 | 2008-12-27 |    1000.00 | Carter   |
|    1 | 2008-12-29 |    1000.00 | Bush     |
+------+------------+------------+----------+
7 rows in set (0.04 sec)

2.以字母顺序显示Customer名称,并以数字顺序显示顺序号(OrderDate):

mysql> select * from Orders order by Customer,OrderDate;
+------+------------+------------+----------+
| O_Id | OrderDate  | OrderPrice | Customer |
+------+------------+------------+----------+
|    6 | 2008-11-22 |    1000.00 | Adams    |
|    5 | 2008-11-29 |    1000.00 | Bush     |
|    2 | 2008-12-24 |    1000.00 | Bush     |
|    1 | 2008-12-29 |    1000.00 | Bush     |
|    1 | 2008-07-22 |    1000.00 | Carter   |
|    4 | 2008-12-27 |    1000.00 | Carter   |
|    3 | 2008-12-26 |    1000.00 | tom      |
+------+------------+------------+----------+
7 rows in set (0.00 sec)

3.以逆字母顺序显示Customer名称:

mysql> select * from Orders order by Customer desc ;
+------+------------+------------+----------+
| O_Id | OrderDate  | OrderPrice | Customer |
+------+------------+------------+----------+
|    3 | 2008-12-26 |    1000.00 | tom      |
|    1 | 2008-07-22 |    1000.00 | Carter   |
|    4 | 2008-12-27 |    1000.00 | Carter   |
|    1 | 2008-12-29 |    1000.00 | Bush     |
|    2 | 2008-12-24 |    1000.00 | Bush     |
|    5 | 2008-11-29 |    1000.00 | Bush     |
|    6 | 2008-11-22 |    1000.00 | Adams    |
+------+------------+------------+----------+
7 rows in set (0.00 sec)

4.以逆字母顺序显示Customer名称,并以数字顺序显示顺序号OrderDate:

mysql> select * from Orders order by Customer desc, OrderDate asc ;
+------+------------+------------+----------+
| O_Id | OrderDate  | OrderPrice | Customer |
+------+------------+------------+----------+
|    3 | 2008-12-26 |    1000.00 | tom      |
|    1 | 2008-07-22 |    1000.00 | Carter   |
|    4 | 2008-12-27 |    1000.00 | Carter   |
|    5 | 2008-11-29 |    1000.00 | Bush     |
|    2 | 2008-12-24 |    1000.00 | Bush     |
|    1 | 2008-12-29 |    1000.00 | Bush     |
|    6 | 2008-11-22 |    1000.00 | Adams    |
+------+------------+------------+----------+
7 rows in set (0.00 sec)

7.ORDER BY 子句的更多相关文章

  1. TSQL order by 子句中排序列的多种写法

    Order by 子句用于对结果进行排序,执行顺序位于select子句之后,排序列有4中写法: column_name column_alias,由于order by子句的执行顺序位于select子句 ...

  2. sql:除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询

    执行sql语句: select * from ( select * from tab where ID>20 order by userID desc ) as a order by date ...

  3. T-SQL ORDER BY子句 排序方式

    MS SQL Server ORDER BY子句用于根据一个或多个列以升序或降序对数据进行排序. 默认情况下,一些数据库排序查询结果按升序排列. 语法 以下是ORDER BY子句的基本语法. SELE ...

  4. ORDER BY 子句在视 图、内联函数、派生表、子查询和公用表表达式中无效

    SQL语句: select * from (select distinct t2.issue,cashmoney from (select distinct issue from lot_gamepa ...

  5. 1. 安装Oracle,配置环境 2. 实现查询From子句 3. 实现查询where子句 4. 实现查询order by子句

    一.环境安装1. 登录:以管理员身份登录 sqlplus 登录名/密码 管理员身份登录:sqlplus system/1234562. 登录后,导入案例.下载scott.sql文件,执行下面一行的命令 ...

  6. [转]sql:除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询

    执行sql语句: select * from ( select * from tab where ID>20 order by userID desc ) as a order by date ...

  7. MySQL的order by子句

    1.语法:select 字段列表 from 表名 [where 子句][group by 子句][having 子句][order by 子句]; 注解: 1.默认是从第一条记录开始升序, 2.des ...

  8. [sql Server]除非另外还指定了TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效

    今天遇到一个奇怪的问题,项目突然要从mysql切换到sql server数据库,包含order by 子句的嵌套子查询报错. 示例:select top 10 name,age,sex from ( ...

  9. Mysql查询语句的 where子句、group by子句、having子句、order by子句、limit子句

    Mysql的各个查询语句 一.where子句   语法:select *|字段列表 from 表名 where 表达式.where子句后面往往配合MySQL运算符一起使用(做条件判断) 作用:通过限定 ...

  10. MySQL中列别名为中文时,Order by 子句中使用别名时不要加引号

    暂时还不清楚原因 1.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 SC表: 这里,当做总成绩处理 select sid, sum(score) as '总成绩', avg(score) ...

随机推荐

  1. 剑指offer-第五章优化时间和空间效率(从1到n的整数中1出现的次数)

    题目:输入一个整数n,从1到n这n个十进制整数中1出现的次数. 思路1:对1到n中的任意一个数i对其进行求余数来判断个位是否为1,然后再求除数,判断十位是否为1.统计出1的个数.然后对1到n用一个循环 ...

  2. CSS命名规范参考及书写注意事项

    CSS书写顺序 *{ /*显示属性*/ display position float clear cursor … /*盒模型*/ margin padding width height /*排版*/ ...

  3. fn project 运行时配置选项

    Env Variables Description Default values DB_URL The database URL to use in URL format. SeeDatabases  ...

  4. 如何给 FastAdmin 单独设置域名

    如何给 FastAdmin 单独设置域名 (声明:不建议给后台固定的域名,主要是安全问题) FastAdmin 是基于 ThinkPHP5 框架编写的,ThinkPHP 5 支持域名路由,可对模块单独 ...

  5. 【转】Python自动化测试 (一) Eclipse+Pydev 搭建开发环境

    原文网址:http://www.cnblogs.com/TankXiao/archive/2013/05/29/3033640.html C#之所以容易让人感兴趣,是因为安装完Visual Studi ...

  6. React组件传值方式总结

    1. 子组件向父组件传值 父组件Header: import Nav from 'Nav.js'; class Header extends React.Component { constructor ...

  7. Linux环境变量从用户配置改为系统配置

    部署了一个新的tomcat到一个新的用户下,发下启动失败了 /home/personal/apache-tomcat/bin/catalina.sh: line 434: /usr/lib/jvm/j ...

  8. 实现对sqlite数据库增删改查

    package com.example.db.dao;import java.util.ArrayList;import java.util.List;import android.content.C ...

  9. laravel中好用的支付安装包

    是包括支付宝和微信的支付 准用包,在测试中 https://github.com/yansongda/laravel-pay 这个包,看上去很好但是composer require时,要求php太高, ...

  10. yii console

    Here is a step by step to show how to run command in the server with yii framework. 1. Create the we ...