mysql 指定数字排序_Mysql数据排序

排序数据

普通字段排序

按照单一字段排序

按照多个字段排序

手动指定排序顺序

单个字段手动排序

多个字段手动排序

普通字段排序

按照单一字段排序

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

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 |

+-------+--------+-----------+------+------------+---------+---------+--------+

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 |

+-------+-------+---------+------+------------+---------+------+--------+

按照多个字段排序

按照多个字段排序,按前后顺序,先出现的先进行排序。如:首先进行job排序,再进行sal排序。

select * from emp order by job,sal;

| 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 |

| 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 |

| 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 |

| 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 |

| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.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 |

| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 |

| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 |

+-------+--------+-----------+------+------------+---------+---------+--------+

手动指定排序顺序

单个字段手动排序

默认是升序。怎么指定升序或者降序呢?asc表示升序,desc表示降序。

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 |

+-------+--------+-----------+------+------------+---------+---------+--------+

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 |

+-------+--------+-----------+------+------------+---------+---------+--------+

多个字段手动排序

按照工资的降序排列,当工资相同的时候再按照名字的升序排列。

select * from emp order by job desc, sal desc;

| EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO |

+-------+--------+-----------+------+------------+---------+---------+--------+

| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |

| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |

| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 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 |

| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |

| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.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 |

+-------+--------+-----------+------+------------+---------+---------+--------+

越靠前的字段越能起到主导作用。只有当前面的字段无法完成排序的时候,才会启用后面的字段。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/542707.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

《黃帝內經 —— 央視60集紀錄片》

下載地址: http://pan.baidu.com/s/1dFI8hxf 目錄 第一部 医史篇第1集:神奇的秘笈(《黄帝内经》是部什么书)第2集:赫赫始祖(上)(黄帝、炎帝)第3集:赫赫始祖&a…

mnist手写数字数据集_mnist手写数据集(1. 加载与可视化)

》》欢迎 点赞,留言,收藏加关注《《1. 模型构建的步骤:在构建AI模型时,一般有以下主要步骤:准备数据、数据预处理、划分数据集、配置模型、训练模型、评估优化、模型应用,如下图所示:【注意】由…

python凯撒密码实现_密码:凯撒密码及其Python实现

python凯撒密码实现Before we start let’s some basic terminology... 在开始之前,让我们先介绍一些基本术语... The art and science to achieve security by encoding messages to make them unreadable are known as Cryptography. That’s what the whole art…

qtextedit 默认文案_QT-纯代码控件-QSplitter(分裂器)

版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接和本声明。本文链接:https://blog.csdn.net/qq_41488943/article/details/96431379使用Qplitter实现页面的三布局分布1.新建一个无ui界面的工程&…

TYVJ P1030 乳草的入侵 Label:跳马问题

背景 USACO OCT09 6TH描述 Farmer John一直努力让他的草地充满鲜美多汁的而又健康的牧草。可惜天不从人愿&#xff0c;他在植物大战人类中败下阵来。邪恶的乳草已经在他的农场的西北部份佔领了一片立足之地。草地像往常一样&#xff0c;被分割成一个高度為Y(1 < y < 100)…

kotlin中既继承又实现_Kotlin程序| 解决继承中的主要冲突的示例

kotlin中既继承又实现继承中的主要冲突 (Overriding Conflicts in Inheritance) It may appear, we inherit more than one implementation of the same method. 看来&#xff0c;我们继承了同一方法的多个实现。 Need to implement all the methods which we have inherited f…

python雷达图详解_Python简单雷达图绘制

import numpy as np import matplotlib.pyplot as plt import matplotlib matplotlib.rcParams[font.family] SimHei matplotlib.rcParams[font.sans-serif] [SimHei] lables np.array([综合,KDA,发育,推进,生存,输出]) nAttr 6 date np.array([7, 5, 6, 9, 8, 7]) angles…

浏览器兼容问题 透明度 position:fixed bootstrap

浏览器兼容问题&#xff1a;主要是ie8以下&#xff1a; 用bootstrap框架结合jq写页面&#xff0c;因为bootstrap有好多media和html5所以要在引入样式后引入两个js <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --><!-- WARNI…

s查找mysql服务_MySQL菜鸟实录(一):MySQL服务安装实战

CentOS 7基本信息系统版本&#xff1a; CentOS 7.3 64bit系统配置&#xff1a; 4vCPUs | 8GB磁盘空间&#xff1a;[rootecs-ce5a-0001 ~]# df -hFilesystem Size Used Avail Use% Mounted on/dev/vda1 40G 17G 22G 44% /devtmpfs 3.9G 0 3.9G 0% /devtmpfs 3.9G 0 3.9G 0% /dev…

实验一 线性表的顺序存储与实现_【自考】数据结构中的线性表,期末不挂科指南,第2篇

线性表这篇博客写的是线性表相关的内容&#xff0c;包括如下部分&#xff0c;先看下有木有期待啥是线性表线性表的顺序存储线性表的基本运算在顺序表上的实现线性表的链式存储线性表的基本运算在单链表上的实现循环链表与双向循环链表Over&#xff0c;内容还蛮多的&#xff01;…

二叉树打印叶子节点,非递归_使用递归打印链接列表中的备用节点

二叉树打印叶子节点,非递归Solution: 解&#xff1a; Input: A singly linked list whose address of the first node is stored in a pointer, say head 输入&#xff1a;一个单链表 &#xff0c;其第一个节点的地址存储在指针中&#xff0c;例如head Output: The alternati…

TYVJ P1012 火柴棒等式 Label:枚举

背景 NOIP2008年提高组第二题描述 给你n根火柴棍&#xff0c;你可以拼出多少个形如“ABC”的等式&#xff1f;等式中的A、B、C是用火柴棍拼出的整数&#xff08;若该数非零&#xff0c;则最高位不能是0&#xff09;。用火柴棍拼数字0-9的拼法如图所示&#xff1a;注意&#xff…

java math max_Java Math类静态double max(double d1,double d2)示例

java math max数学类静态double max(double d1&#xff0c;double d2) (Math Class static double max(double d1,double d2) ) This method is available in java.lang package. 此方法在java.lang包中可用。 This method is used to return the maximum one of both the give…

python怎么开发软件_怎么使用python进行软件开发

一、下载pyinstaller 我使用的版本为PyInstaller-2.1&#xff0c;支持python版本2.3-2.7&#xff0c;点击这里下载。 二、安装pyinstaller 下载完成后&#xff0c;解压即可。我的解压目录为D:\Python27\PyInstaller-2.1\ 三、使用pyinstaller打包.py成.exe应用程序 1.注意使用前…

28、清华大学脑机接口实验组SSVEP数据集:通过视觉触发BCI[飞一般的赶脚!]

前言&#xff1a; 哈喽&#xff0c;最近对清华大学脑机接口的数据进行了尝试&#xff0c;输入到了DL模型中&#xff0c;以下是本人对于清华BCI数据的个人见解。 数据地址&#xff1a; 清华大学脑机接口研究组 (tsinghua.edu.cn) 打开网站可以看到有很多个数据&#xff0c;官…

python Pexpect

http://www.cnblogs.com/dkblog/archive/2013/03/20/2970738.htmlhttp://www.ibm.com/developerworks/cn/linux/l-cn-pexpect2/index.htmlhttp://www.cnblogs.com/dkblog/archive/2013/03/20/2970738.htmlpython Pexpect Pexpect 是一个用来启动子程序并对其进行自动控制的纯 P…

python 幂运算 整数_在Python中检查一个数字是否是另一个数字的幂

python 幂运算 整数To solve this problem simply, we will use the log() function from the math module. The math module provides us various mathematical operations and here we will use the log() function from this module. In Python working of log() function, …

3dmax镜像后模型线条乱了_3dMax入门教程来啦!小白赶紧收藏!

3D Studio Max&#xff0c;常简称为3d Max或3ds MAX&#xff0c;是Discreet公司开发的&#xff08;后被Autodesk公司合并&#xff09;基于PC系统的三维动画渲染和制作软件&#xff0c; 3dmax软件主要功能有建模&#xff0c;动画&#xff0c;渲染&#xff0c;特效等&#xff0c;…

java中哲学家就餐死锁_哲学家就餐问题与死锁总结

死锁的四个条件&#xff1a;(1) 互斥条件&#xff1a;一个资源每次只能被一个进程使用。(2) 请求与保持条件&#xff1a;一个进程因请求资源而阻塞时&#xff0c;对已获得的资源保持不放。(3) 不剥夺条件:进程已获得的资源&#xff0c;在末使用完之前&#xff0c;不能强行剥夺。…

linux扫描工具之nmap

Linux下有很多强大网络扫描工具&#xff0c;网络扫描工具可以分为&#xff1a;主机扫描、主机服务扫描、路由扫描等,nmap支持批量主机扫描和主机服务扫描。检测安装&#xff1a;[rootbier ~]# rpm -qa nmap nmap-5.51-4.el6.x86_64如果没有安装就安装一下nmap的安装直接使用&am…