二丫讲梵 二丫讲梵
首页
  • 最佳实践
  • 迎刃而解
  • Nginx
  • Php
  • Zabbix
  • AWS
  • Prometheus
  • Grafana
  • CentOS
  • Systemd
  • Docker
  • Rancher
  • Ansible
  • Ldap
  • Gitlab
  • GitHub
  • Etcd
  • Consul
  • RabbitMQ
  • Kafka
  • MySql
  • MongoDB
  • OpenVPN
  • KVM
  • VMware
  • Other
  • ELK
  • K8S
  • LLM
  • Nexus
  • Jenkins
  • 随写编年
  • 家人物语
  • 追忆青春
  • 父亲的朋友圈
  • 电影音乐
  • 效率工具
  • 博客相关
  • Shell
  • 前端实践
  • Vue学习笔记
  • Golang学习笔记
  • Golang编程技巧
  • 学习周刊
  • Obsidian插件周刊
关于
友链
  • 本站索引

    • 分类
    • 标签
    • 归档
  • 本站页面

    • 导航
    • 打赏
  • 我的工具

    • 备忘录清单 (opens new window)
    • json2go (opens new window)
    • gopher (opens new window)
    • 微信MD编辑 (opens new window)
    • 国内镜像 (opens new window)
    • 出口IP查询 (opens new window)
    • 代码高亮工具 (opens new window)
  • 外站页面

    • 开往 (opens new window)
    • ldapdoc (opens new window)
    • HowToStartOpenSource (opens new window)
    • vdoing-template (opens new window)
GitHub (opens new window)

二丫讲梵

行者常至,为者常成
首页
  • 最佳实践
  • 迎刃而解
  • Nginx
  • Php
  • Zabbix
  • AWS
  • Prometheus
  • Grafana
  • CentOS
  • Systemd
  • Docker
  • Rancher
  • Ansible
  • Ldap
  • Gitlab
  • GitHub
  • Etcd
  • Consul
  • RabbitMQ
  • Kafka
  • MySql
  • MongoDB
  • OpenVPN
  • KVM
  • VMware
  • Other
  • ELK
  • K8S
  • LLM
  • Nexus
  • Jenkins
  • 随写编年
  • 家人物语
  • 追忆青春
  • 父亲的朋友圈
  • 电影音乐
  • 效率工具
  • 博客相关
  • Shell
  • 前端实践
  • Vue学习笔记
  • Golang学习笔记
  • Golang编程技巧
  • 学习周刊
  • Obsidian插件周刊
关于
友链
  • 本站索引

    • 分类
    • 标签
    • 归档
  • 本站页面

    • 导航
    • 打赏
  • 我的工具

    • 备忘录清单 (opens new window)
    • json2go (opens new window)
    • gopher (opens new window)
    • 微信MD编辑 (opens new window)
    • 国内镜像 (opens new window)
    • 出口IP查询 (opens new window)
    • 代码高亮工具 (opens new window)
  • 外站页面

    • 开往 (opens new window)
    • ldapdoc (opens new window)
    • HowToStartOpenSource (opens new window)
    • vdoing-template (opens new window)
GitHub (opens new window)
  • 最佳实践

  • 迎刃而解

  • Nginx

  • Php

  • Zabbix

  • AWS

  • Prometheus

  • Grafana

  • Loki

  • CentOS

  • Supervisord

  • Systemd

  • Docker

  • Docker-Compose

  • Rancher

  • Ansible

  • OpenLdap

  • GitLab

  • GitHub

  • Etcd

  • Consul

  • RabbitMQ

  • Kafka

  • Mysql

    • MySQL数据库增量备份的操作
    • MySQL中order by的学习使用
    • MongoDB

    • OpenVPN

    • Kvm

    • VMware

    • 配置文件详解

    • Other

    • 运维观止
    • Mysql
    二丫讲梵
    2020-10-24
    目录

    MySQL中order by的学习使用

    文章发布较早,内容可能过时,阅读注意甄别。

    order by 用于对查询出来的数据进行排序操作,用两个例子可以很容易理解这个参数。

    # 1,普通查询。

    mysql> select * from groups;
    +----+---------------------+---------------------+---------------------+----------+------------+
    | id | created_at          | updated_at          | deleted_at          | group_id | group_name |
    +----+---------------------+---------------------+---------------------+----------+------------+
    |  1 | NULL                | 2020-06-16 22:07:02 | 2020-06-16 22:14:40 |        1 | 开发组     |
    |  2 | 2020-06-15 23:37:25 | 2020-06-15 23:37:25 | NULL                |        2 | 测试组     |
    |  3 | 2020-06-16 22:17:07 | 2020-06-16 22:17:07 | NULL                |        3 | 运营组     |
    |  4 | 2020-06-16 22:17:15 | 2020-06-16 22:17:15 | NULL                |        4 | 客服组     |
    |  5 | 2020-06-16 22:17:26 | 2020-06-16 22:17:26 | NULL                |        5 | OPS        |
    +----+---------------------+---------------------+---------------------+----------+------------+
    5 rows in set (0.00 sec)
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

    默认情况下,查出来的数据是按照主键 ID 正序排列的。

    img

    # 2,基于 ID 进行排序。

    mysql> select * from groups order by id desc;
    +----+---------------------+---------------------+---------------------+----------+------------+
    | id | created_at          | updated_at          | deleted_at          | group_id | group_name |
    +----+---------------------+---------------------+---------------------+----------+------------+
    |  5 | 2020-06-16 22:17:26 | 2020-06-16 22:17:26 | NULL                |        5 | OPS        |
    |  4 | 2020-06-16 22:17:15 | 2020-06-16 22:17:15 | NULL                |        4 | 客服组     |
    |  3 | 2020-06-16 22:17:07 | 2020-06-16 22:17:07 | NULL                |        3 | 运营组     |
    |  2 | 2020-06-15 23:37:25 | 2020-06-15 23:37:25 | NULL                |        2 | 测试组     |
    |  1 | NULL                | 2020-06-16 22:07:02 | 2020-06-16 22:14:40 |        1 | 开发组     |
    +----+---------------------+---------------------+---------------------+----------+------------+
    5 rows in set (0.00 sec)
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

    注意其中的 desc表示用倒序排列。

    如果想要正序排列,则使用asc:

    mysql> select * from groups order by id asc;
    +----+---------------------+---------------------+---------------------+----------+------------+
    | id | created_at          | updated_at          | deleted_at          | group_id | group_name |
    +----+---------------------+---------------------+---------------------+----------+------------+
    |  1 | NULL                | 2020-06-16 22:07:02 | 2020-06-16 22:14:40 |        1 | 开发组     |
    |  2 | 2020-06-15 23:37:25 | 2020-06-15 23:37:25 | NULL                |        2 | 测试组     |
    |  3 | 2020-06-16 22:17:07 | 2020-06-16 22:17:07 | NULL                |        3 | 运营组     |
    |  4 | 2020-06-16 22:17:15 | 2020-06-16 22:17:15 | NULL                |        4 | 客服组     |
    |  5 | 2020-06-16 22:17:26 | 2020-06-16 22:17:26 | NULL                |        5 | OPS        |
    +----+---------------------+---------------------+---------------------+----------+------------+
    5 rows in set (0.00 sec)
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    微信 支付宝
    #mysql
    上次更新: 2024/07/04, 22:40:37
    MySQL数据库增量备份的操作
    认识了解MongoDB

    ← MySQL数据库增量备份的操作 认识了解MongoDB→

    最近更新
    01
    记录二五年五一之短暂回归家庭
    05-09
    02
    学习周刊-总第210期-2025年第19周
    05-09
    03
    学习周刊-总第209期-2025年第18周
    05-03
    更多文章>
    Theme by Vdoing | Copyright © 2017-2025 | 点击查看十年之约 | 浙ICP备18057030号
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式