首页 > 数据库 >MySQL基础篇:掌握MySQL数据排序,让你的数据分析事半功倍

MySQL基础篇:掌握MySQL数据排序,让你的数据分析事半功倍

时间:2023-09-10 23:35:49浏览次数:49  
标签:数据分析 20 1981 30 7839 事半功倍 MySQL NULL 7698

MySQL基础篇:掌握MySQL数据排序,让你的数据分析事半功倍_字段

单一字段排序

排序采用order by子句,order by后面跟上排序字段,排序字段可以放多个,多个采用逗号间隔,order by默认采用升序,如果存在where子句那么order by必须放到where语句的后面

按照薪水由小到大排序(系统默认由小到大)

mysql> select * from EMP order by SAL;
+-------+--------+-----------+------+------------+---------+---------+--------+
| EMPNO | ENAME  | JOB       | MGR  | HIREDATE   | SAL     | COMM    | DEPTNO |
+-------+--------+-----------+------+------------+---------+---------+--------+
|  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800.00 | NULL    |     20 |
|  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950.00 | NULL    |     30 |
|  7876 | ADAMS  | CLERK     | 7788 | 1987-05-23 | 1100.00 | NULL    |     20 |
|  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250.00 |  500.00 |     30 |
|  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250.00 | 1400.00 |     30 |
|  7934 | MILLER | CLERK     | 7782 | 1982-01-23 | 1300.00 | NULL    |     10 |
|  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500.00 |    0.00 |     30 |
|  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600.00 |  300.00 |     30 |
|  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450.00 | NULL    |     10 |
|  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 | 2850.00 | NULL    |     30 |
|  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975.00 | NULL    |     20 |
|  7788 | SCOTT  | ANALYST   | 7566 | 1987-04-19 | 3000.00 | NULL    |     20 |
|  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000.00 | NULL    |     20 |
|  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL    |     10 |
+-------+--------+-----------+------+------------+---------+---------+--------+
14 rows in set (0.29 sec)

取得job为MANAGER的员工,按照薪水由小到大排序(系统默认由小到大)

mysql> select * from EMP where JOB="MANAGER" order by SAL;
+-------+-------+---------+------+------------+---------+------+--------+
| EMPNO | ENAME | JOB     | MGR  | HIREDATE   | SAL     | COMM | DEPTNO |
+-------+-------+---------+------+------------+---------+------+--------+
|  7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL |     10 |
|  7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL |     30 |
|  7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL |     20 |
+-------+-------+---------+------+------------+---------+------+--------+
3 rows in set (0.25 sec)

如果包含where语句order by必须放到where后面,如果没有where语句order by放到表的后面。

以下写法是错误的:

select * from EMP order by SAL where JOB='MANAGER';

按照多个字段排序,如:首先按照job排序,再按照sal排序

select * from EMP order by JOB,SAL;

手动指定排序顺序

手动指定按照薪水由小到大排序.

mysql> select * from EMP order by SAL ASC;
+-------+--------+-----------+------+------------+---------+---------+--------+
| EMPNO | ENAME  | JOB       | MGR  | HIREDATE   | SAL     | COMM    | DEPTNO |
+-------+--------+-----------+------+------------+---------+---------+--------+
|  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800.00 | NULL    |     20 |
|  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950.00 | NULL    |     30 |
|  7876 | ADAMS  | CLERK     | 7788 | 1987-05-23 | 1100.00 | NULL    |     20 |
|  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250.00 |  500.00 |     30 |
|  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250.00 | 1400.00 |     30 |
|  7934 | MILLER | CLERK     | 7782 | 1982-01-23 | 1300.00 | NULL    |     10 |
|  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500.00 |    0.00 |     30 |
|  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600.00 |  300.00 |     30 |
|  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450.00 | NULL    |     10 |
|  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 | 2850.00 | NULL    |     30 |
|  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975.00 | NULL    |     20 |
|  7788 | SCOTT  | ANALYST   | 7566 | 1987-04-19 | 3000.00 | NULL    |     20 |
|  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000.00 | NULL    |     20 |
|  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL    |     10 |
+-------+--------+-----------+------+------------+---------+---------+--------+
14 rows in set (0.26 sec)

手动指定按照薪水由大到小排序.

mysql> select * from EMP order by SAL desc;
+-------+--------+-----------+------+------------+---------+---------+--------+
| EMPNO | ENAME  | JOB       | MGR  | HIREDATE   | SAL     | COMM    | DEPTNO |
+-------+--------+-----------+------+------------+---------+---------+--------+
|  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL    |     10 |
|  7788 | SCOTT  | ANALYST   | 7566 | 1987-04-19 | 3000.00 | NULL    |     20 |
|  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000.00 | NULL    |     20 |
|  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975.00 | NULL    |     20 |
|  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 | 2850.00 | NULL    |     30 |
|  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450.00 | NULL    |     10 |
|  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600.00 |  300.00 |     30 |
|  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500.00 |    0.00 |     30 |
|  7934 | MILLER | CLERK     | 7782 | 1982-01-23 | 1300.00 | NULL    |     10 |
|  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250.00 |  500.00 |     30 |
|  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250.00 | 1400.00 |     30 |
|  7876 | ADAMS  | CLERK     | 7788 | 1987-05-23 | 1100.00 | NULL    |     20 |
|  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950.00 | NULL    |     30 |
|  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800.00 | NULL    |     20 |
+-------+--------+-----------+------+------------+---------+---------+--------+
14 rows in set (0.25 sec)

多个字段排序

按照job和薪水倒序。

mysql> select * from EMP order by JOB,SAL desc;
+-------+--------+-----------+------+------------+---------+---------+--------+
| EMPNO | ENAME  | JOB       | MGR  | HIREDATE   | SAL     | COMM    | DEPTNO |
+-------+--------+-----------+------+------------+---------+---------+--------+
|  7788 | SCOTT  | ANALYST   | 7566 | 1987-04-19 | 3000.00 | NULL    |     20 |
|  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000.00 | NULL    |     20 |
|  7934 | MILLER | CLERK     | 7782 | 1982-01-23 | 1300.00 | NULL    |     10 |
|  7876 | ADAMS  | CLERK     | 7788 | 1987-05-23 | 1100.00 | NULL    |     20 |
|  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950.00 | NULL    |     30 |
|  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800.00 | NULL    |     20 |
|  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975.00 | NULL    |     20 |
|  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 | 2850.00 | NULL    |     30 |
|  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450.00 | NULL    |     10 |
|  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL    |     10 |
|  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600.00 |  300.00 |     30 |
|  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500.00 |    0.00 |     30 |
|  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250.00 |  500.00 |     30 |
|  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250.00 | 1400.00 |     30 |
+-------+--------+-----------+------+------------+---------+---------+--------+
14 rows in set (0.28 sec)

如果采用多个字段排序,如果根据第一个字段排序重复了,会根据第二个字段排序。


使用字段的位置来排序

按照薪水升序

mysql> select * from EMP order by 6;
+-------+--------+-----------+------+------------+---------+---------+--------+
| EMPNO | ENAME  | JOB       | MGR  | HIREDATE   | SAL     | COMM    | DEPTNO |
+-------+--------+-----------+------+------------+---------+---------+--------+
|  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800.00 | NULL    |     20 |
|  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950.00 | NULL    |     30 |
|  7876 | ADAMS  | CLERK     | 7788 | 1987-05-23 | 1100.00 | NULL    |     20 |
|  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250.00 |  500.00 |     30 |
|  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250.00 | 1400.00 |     30 |
|  7934 | MILLER | CLERK     | 7782 | 1982-01-23 | 1300.00 | NULL    |     10 |
|  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500.00 |    0.00 |     30 |
|  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600.00 |  300.00 |     30 |
|  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450.00 | NULL    |     10 |
|  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 | 2850.00 | NULL    |     30 |
|  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975.00 | NULL    |     20 |
|  7788 | SCOTT  | ANALYST   | 7566 | 1987-04-19 | 3000.00 | NULL    |     20 |
|  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000.00 | NULL    |     20 |
|  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL    |     10 |
+-------+--------+-----------+------+------------+---------+---------+--------+
14 rows in set (0.33 sec)

标签:数据分析,20,1981,30,7839,事半功倍,MySQL,NULL,7698
From: https://blog.51cto.com/feng1992/7428787

相关文章

  • MySQL入门系列7-多表查询
    在我们日常开发中,表与表之间的关系通常分为以下几种:一对多(多对一)、’多对多、一对一。一、一对多(多对一)我们之前的部门和员工表就是一个一对多的关系,一个部门有多个员工,部门就是一方,员工就是多方。员工表的外键关联了部门表二、多对多学生和课程的关系可以理解为多对多的关系,......
  • MYSQL基础上
    MYSQL基础确保MySQL已经安装完成启动windows下进入cmd的管理运行模式启动netstartmysql80停止netstopmysql80连接客户端连接注意这里使用的命令行既然在所有目录下都可行,那么必然要改环境变量数据模型SQLDDLDDL-数据库操作查询查询所有数据库SHOWDATAB......
  • MySQL数据库进阶 自定义函数
    自定义函数在MySQL中,您可以使用自定义函数来扩展数据库管理系统的功能。自定义函数允许您封装一段可重用的代码,并在查询和其他操作中调用它。以下是在MySQL中创建和使用自定义函数的一般步骤:1、创建自定义函数语法:CREATEFUNCTIONfunction_name(parameters)RETURNSreturn_t......
  • Navicat连接Docker创建的MySQL运行实例
    一步步教你在Docker中安装MySQL(qq.com)Docker部署MySql应用-魔鬼YU天使-博客园(cnblogs.com)拉取MySQL镜像我们可以通过Docker提供的命令,直接从DockerHub上拉取MySQL的官方镜像。打开终端,输入如下命令:dockerpullmysql:8.0其中,:8.0表示我们需要下载的MySQL版本,你也......
  • linux7.X二进制方式安装MySQL5.7.X
    概述系统环境服务器型号VMwareVirtualPlatform操作系统版本RHEL7.6x86_64主机名linuxpg51PubIP192.168.115.51数据库安装版本mysqlVer14.14Distrib5.7.30,forlinux-glibc2.12(x86_64)usingEditLinewrapper安装路径规划[mysql@linuxpg51/]$tree-L1/mysql/mysq......
  • 深入浅出理解数据分析系列之:Python安装Excel文档库openpyxl和Pycharm为项目安装Excel
    深入浅出理解数据分析系列之:Python安装Excel文档库openpyxl和Pycharm为项目安装Excel文档库openpyxl一、Python安装openpyxl二、Pycharm为项目安装openpyxl一、Python安装openpyxlpip3installopenpyxlCollectingopenpyxlDownloadingopenpyxl-3.0.9-py2.py3-none-any.whl......
  • MySQL基础
    要学习数据库首先要先搞清楚三个概念数据库(DB):是存储数据的仓库数据库管理系统(DBMS):管理数据库的大型软件SQL:通过SQL操作数据库管理系统操作数据库,对数据库进行增删改查等由此我们可以知道数据库就是安装在操作系统之上的数据仓库,用于存储数据。我们也先认识一个概念->关系型数据......
  • 【k8s】k8s构建mysql双主集群
    背景当前很多开源系统都是基于k8s,而部署时会遇到组件包含mysql的情况。理想的情况下,是将mysql迁移到云上托管。但实际情况可能比较复杂,比如这个开源框架需要mysql的DML权限,但是公司DBA不提供;而基于k8s部署mysql的问题是,这个mysql集群往往是单点的。一般mysql会依赖于一个本地挂......
  • Windows安装MySQL Community Server 8.1.0 Innovation
    检查是否安装按win+r打开运行,输入services.msc回车在服务列表中查找mysql服务,如果存在mysql服务,说明已经安装mysql。反之,则说明没有安装了mysql服务。下载并安装下载链接:https://dev.mysql.com/downloads/mysql/MySQLCommunityServer8.1.0Innovation有msi版本......
  • 【高级】MySQL主从复制
    【一】引入之前做过redis的主从,很简单mysql稍微复杂一些,搭建mysql主从的目的是?读写分离单个实例并发量低,提高并发量只在主库写,读数据都去从库----》读多写少读写分离通过将读操作分发到从库,可以分摊主库的读负载,提高整体的并发处理能力。主库只需处理写操作,从库......