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) ...
随机推荐
- 计算机信息类ComputerInfo
using System; using System.Management; using System.Net; using System.Net.Sockets; using System.Text ...
- restful规则
参考连接:https://blog.igevin.info/posts/restful-api-get-started-to-write/#url_rules https://juejin.im/po ...
- Xcode 打开playground文件的时候提示-Unable to find execution service for selected run destination
解决办法: step 1: 关闭Xcode (快捷键cmd + q) step 2:在terminal里面运行如下语句 rm -rf ~/Library/Developer/CoreSimulator ...
- python 函数 hex()
hex(x)作用:hex() 函数用于将10进制整数转换成16进制整数. x-10进制整数,返回16进制整数 实例: >>>hex(255) '0xff' >>> ...
- php执行多个存储过程(二)
使用公共连接文件形式 include_once 'inc/conni.php'; $result = mysqli_query($conni, "call proc_test()" ...
- bzoj 1597 [Usaco2008 Mar]土地购买——斜率优化dp
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1597 又一道斜率优化dp.负数让我混乱.不过仔细想想还是好的. 还可以方便地把那个负号放到x ...
- Eclipse执行import命令导入maven项目时报错:Add a version or custom suffix using "Name template" in "Advanced" settings
新建了两个maven项目在E盘workspace目录,后面移到workspace/app_engine目录下提交svn,再通过Eclipse的File->import导入时报错了: Projec ...
- DHCP(六)
DHCP报文: DHCP共有八种报文,分别为DHCP Discover.DHCP Offer.DHCP Request.DHCP ACK.DHCP NAK.DHCP Release.DHCP Decl ...
- 【转】Jmeter在命令行运行技巧
For non-interactive testing, you may choose to run JMeter without the GUI. To do so, use the followi ...
- 安装appium需要注意的事项
参考 虫师 的博客园 :http://www.cnblogs.com/fnng/p/4560298.html 1.其中第二篇中,打开命令行用的不是windows中的cmd打开的界面,而是用node. ...