7.ORDER BY 子句
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 子句的更多相关文章
- TSQL order by 子句中排序列的多种写法
Order by 子句用于对结果进行排序,执行顺序位于select子句之后,排序列有4中写法: column_name column_alias,由于order by子句的执行顺序位于select子句 ...
- sql:除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询
执行sql语句: select * from ( select * from tab where ID>20 order by userID desc ) as a order by date ...
- T-SQL ORDER BY子句 排序方式
MS SQL Server ORDER BY子句用于根据一个或多个列以升序或降序对数据进行排序. 默认情况下,一些数据库排序查询结果按升序排列. 语法 以下是ORDER BY子句的基本语法. SELE ...
- ORDER BY 子句在视 图、内联函数、派生表、子查询和公用表表达式中无效
SQL语句: select * from (select distinct t2.issue,cashmoney from (select distinct issue from lot_gamepa ...
- 1. 安装Oracle,配置环境 2. 实现查询From子句 3. 实现查询where子句 4. 实现查询order by子句
一.环境安装1. 登录:以管理员身份登录 sqlplus 登录名/密码 管理员身份登录:sqlplus system/1234562. 登录后,导入案例.下载scott.sql文件,执行下面一行的命令 ...
- [转]sql:除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询
执行sql语句: select * from ( select * from tab where ID>20 order by userID desc ) as a order by date ...
- MySQL的order by子句
1.语法:select 字段列表 from 表名 [where 子句][group by 子句][having 子句][order by 子句]; 注解: 1.默认是从第一条记录开始升序, 2.des ...
- [sql Server]除非另外还指定了TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效
今天遇到一个奇怪的问题,项目突然要从mysql切换到sql server数据库,包含order by 子句的嵌套子查询报错. 示例:select top 10 name,age,sex from ( ...
- Mysql查询语句的 where子句、group by子句、having子句、order by子句、limit子句
Mysql的各个查询语句 一.where子句 语法:select *|字段列表 from 表名 where 表达式.where子句后面往往配合MySQL运算符一起使用(做条件判断) 作用:通过限定 ...
- MySQL中列别名为中文时,Order by 子句中使用别名时不要加引号
暂时还不清楚原因 1.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 SC表: 这里,当做总成绩处理 select sid, sum(score) as '总成绩', avg(score) ...
随机推荐
- C# 操作嵌入的资源
被嵌入的程序执行文件 namespace AppTest { class Program { static void Main(string[] args) { Console.WriteLine(& ...
- Swift-ScrollView轮播图的简易封装和使用
不多说,轮播图是开发中必要一项技能,直接上代码: 先说我的思路:首次继承于UIScrollView类自定义MyScrollView,在MyScrollView里自定制方法,func creatMySc ...
- Linux 系统 LVM(Logical Volume Manager)逻辑卷管理
一.前言 每个Linux使用者在安装Linux时都会遇到这样的困境:在为系统分区时,如何精确评估和分配各个硬盘分区的容量,因为系统管理员不但要考虑到 当前某个分区需要的容量,还要预见该分区以后可能需要 ...
- Ambari的API调用
GET api/v1/clusters/HDP/configurations可以获得所有的配置信息(例如,http://hdp0:8080/api/v1/clusters/HDP/configurat ...
- MySQL插入中文时出现ERROR 1406 (22001): Data too long for column 'name' at row 1 (转)
使用命令行方式登陆到MySQL服务器, 建立一个数据库,数据库编码设为UTF-8.此时,如果直接在命令行窗口使用insert语句插入中文,就遇到类似 ERROR 1406 (22001): Data ...
- php单链表实现
php单链表实现 <?php //单链表 class Hero{ public $no; public $name; public $nickname; public $next=null; f ...
- 蓝桥杯 算法训练 ALGO-60 矩阵乘法
算法训练 矩阵乘方 时间限制:1.0s 内存限制:512.0MB 问题描述 给定一个矩阵A,一个非负整数b和一个正整数m,求A的b次方除m的余数. 其中一个nxn的矩阵除m的余数得到的仍是一个 ...
- Ubuntu apt-get卸载小记
过sudo apt-get install xxxx 安装软件后,总是无法卸载干净,这里以Apache 为例,提供方法:首先sudo apt-get remove apache2再sudo apt-g ...
- 【转】配置Jmeter的自定义参数
配置Jmeter的自定义参数 User Defined Variables 在这个控件中,定义你所需要的参数,如 在对应的需要使用参数的位置,使用${host}替代. 应用场景: 当测试环境变化时,我 ...
- elasticsearch5.6.8中文分词器
安装分词器,务必确保版本一致! 下载地址:https://github.com/medcl/elasticsearch-analysis-ik 为了保证一致,我特地将elasticsearch进行降级 ...