码迷,mamicode.com
首页 > 其他好文 > 详细

7.ORDER BY 子句

时间:2017-02-07 01:11:03      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:_id   date   asc   名称   rom   div   rda   bsp   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 子句

标签:_id   date   asc   名称   rom   div   rda   bsp   order by   

原文地址:http://www.cnblogs.com/yejibigdata/p/6372163.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!