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. Stars

    Astronomers often examine star maps where stars are represented by points on a plane and each star h ...

  2. matlab滤波器的设计

    求出濾波器的階數以及 3dB 截止頻率後,可用相應的 Matlab 函數計算出實現傳遞函數的分子分母係數來.巴特沃斯型濾波器是通帶內最大平坦.帶外單調下降型的,其計算命令是:[b,a] = butte ...

  3. ACM学习历程—SNNUOJ 1239 Counting Star Time(树状数组 && 动态规划 && 数论)

    http://219.244.176.199/JudgeOnline/problem.php?id=1239 这是这次陕西省赛的G题,题目大意是一个n*n的点阵,点坐标从(1, 1)到(n, n),每 ...

  4. 【模板】NOIP模板汇总

    图论 数据结构 数学 其他: 洛谷模板:a,b两个字符串,求b串在a串中出现的位置 #include<iostream> #include<cstdio> #include&l ...

  5. maven依赖顺序原则

    使用maven的程序员都会遇到一个问题,那就是maven依赖冲突的问题,这会导致ClassNotFound或者MethodNotFound这样的异常.其实只要明白maven依赖的根本性的原则就不怕这样 ...

  6. bzoj 3277 & bzoj 3473,bzoj 2780 —— 广义后缀自动机

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3277 https://www.lydsy.com/JudgeOnline/problem.p ...

  7. Eclipse中调试Jar包的源码(调试Struts2源码)

    首先在Eclipse中创建一个新的项目,加入运行Struts2所需要的JAR文件,并将它们加到项目的CLASSPATH中(在Lisbs中右击 build path 如下图: ),成功后的界面如图 1- ...

  8. Oracle查询多边形对象SDO_GEOMETRY并转换为java对象举例

    最近实现了一个判断点是否与多边形交互的功能,这里的点是一个经纬度,多边形是一个区域,包含多个经纬度,最后看下这个点是否在这个区域内.就好比你打开百度地图,然后看你自己的位置(点)是不是在某个小区(多边 ...

  9. Java 数组的浅拷贝和深拷贝

    浅拷贝: 在堆内存中不会分配新的空间,而是增加一个引用变量和之前的引用指向相同的堆空间. int[] a = {1,2,3,4,5}; int[]b = a; public class Test { ...

  10. STM32从boot跳转到app失败

    现象:在每次boot执行完跳转到APP时,都会跑飞 原因:在boot中使用到了USART和TIM,boot执行完没有关闭总中断 方法:在boot执行完跳转之前关闭中断,__disable_irq() ...