zoukankan      html  css  js  c++  java
  • mysql ORDER BY的使用

    新建表如下:

    +----+---------------------+------+----------+
    | id | sale_date           | num  | order_id |
    +----+---------------------+------+----------+
    |  1 | 2020-05-12 23:23:12 |   12 |      100 |
    |  2 | 2020-05-14 23:23:31 |   15 |      100 |
    |  3 | 2020-05-15 23:23:50 |    1 |      100 |
    |  4 | 2020-05-16 23:24:14 |   10 |      101 |
    |  5 | 2020-05-17 23:24:33 | NULL |      101 |
    +----+---------------------+------+----------+

    一 按order_id列默认排序查询:

    mysql> SELECT * FROM order_sale_info ORDER BY order_sale_info.order_id;
    +----+---------------------+------+----------+
    | id | sale_date           | num  | order_id |
    +----+---------------------+------+----------+
    |  1 | 2020-05-12 23:23:12 |   12 |      100 |
    |  2 | 2020-05-14 23:23:31 |   15 |      100 |
    |  3 | 2020-05-15 23:23:50 |    1 |      100 |
    |  4 | 2020-05-16 23:24:14 |   10 |      101 |
    |  5 | 2020-05-17 23:24:33 | NULL |      101 |
    +----+---------------------+------+----------+
    5 rows in set

    可以看到按order_id列查询时默认按升序排列,与添加ASC关键字效果一致。

    mysql> SELECT * FROM order_sale_info ORDER BY order_sale_info.order_id ASC;
    +----+---------------------+------+----------+
    | id | sale_date           | num  | order_id |
    +----+---------------------+------+----------+
    |  1 | 2020-05-12 23:23:12 |   12 |      100 |
    |  2 | 2020-05-14 23:23:31 |   15 |      100 |
    |  3 | 2020-05-15 23:23:50 |    1 |      100 |
    |  4 | 2020-05-16 23:24:14 |   10 |      101 |
    |  5 | 2020-05-17 23:24:33 | NULL |      101 |
    +----+---------------------+------+----------+
    5 rows in set

    二 多列排序查询:

    按order_id 升序,num升序排序效果,每个排序字段后边要跟排序的类型(ASC或者DESC),如果排序类型为ASC可以不写,默认为ASC,如上述一情况。

    mysql> SELECT * FROM order_sale_info ORDER BY order_sale_info.order_id ASC,order_sale_info.num ASC;
    +----+---------------------+------+----------+
    | id | sale_date           | num  | order_id |
    +----+---------------------+------+----------+
    |  3 | 2020-05-15 23:23:50 |    1 |      100 |
    |  1 | 2020-05-12 23:23:12 |   12 |      100 |
    |  2 | 2020-05-14 23:23:31 |   15 |      100 |
    |  5 | 2020-05-17 23:24:33 | NULL |      101 |
    |  4 | 2020-05-16 23:24:14 |   10 |      101 |
    +----+---------------------+------+----------+
    5 rows in set

    按order_id 升序,num降序排序效果,每个排序字段后边要跟排序的类型(ASC或者DESC)。

    mysql> SELECT * FROM order_sale_info ORDER BY order_sale_info.order_id ASC,order_sale_info.num DESC;
    +----+---------------------+------+----------+
    | id | sale_date           | num  | order_id |
    +----+---------------------+------+----------+
    |  2 | 2020-05-14 23:23:31 |   15 |      100 |
    |  1 | 2020-05-12 23:23:12 |   12 |      100 |
    |  3 | 2020-05-15 23:23:50 |    1 |      100 |
    |  4 | 2020-05-16 23:24:14 |   10 |      101 |
    |  5 | 2020-05-17 23:24:33 | NULL |      101 |
    +----+---------------------+------+----------+
    5 rows in set

    如果不写排序类型,默认为升序,如下:

    mysql> SELECT * FROM order_sale_info ORDER BY order_sale_info.order_id,order_sale_info.num DESC;
    +----+---------------------+------+----------+
    | id | sale_date           | num  | order_id |
    +----+---------------------+------+----------+
    |  2 | 2020-05-14 23:23:31 |   15 |      100 |
    |  1 | 2020-05-12 23:23:12 |   12 |      100 |
    |  3 | 2020-05-15 23:23:50 |    1 |      100 |
    |  4 | 2020-05-16 23:24:14 |   10 |      101 |
    |  5 | 2020-05-17 23:24:33 | NULL |      101 |
    +----+---------------------+------+----------+
    5 rows in set

    order_id列未声明排序类型,默认为升序,num声明排序规则未DESC,体现为降序。

  • 相关阅读:
    Socket编程注意接收缓冲区大小
    Ubuntu 14.04环境变量修改
    python模块------json
    解决ssh连接linux系统特别慢的问题
    centos7 安装 ftp 服务及创建 repo源
    qcow2镜像制作
    k8s-helm01-----helm基本使用
    mysql常用sql
    js中自执行函数(function(){})()和(function(){}())区别
    npm镜像指定用淘宝镜像去下载
  • 原文地址:https://www.cnblogs.com/silenceshining/p/13424017.html
Copyright © 2011-2022 走看看