当前位置:网站首页>MySQL implements sorting according to custom (specified order)
MySQL implements sorting according to custom (specified order)
2022-08-02 06:09:00 【web15286201346】
mysql sort according to the specified order (custom sort)
- I. Implement specified sorting requirements in mysql
- Second, the solution
I. Implement specified sorting requirements in mysql
Under normal circumstances, we use the order by field asc/desc directly for sorting; but if the data format of the field to be sorted cannot be directly implemented, or we need to specify the order and there is no rule, a simple order by fieldIt cannot be achieved;
As shown in the figure below;
We need to sort according to the order of Bank of China, China Construction Bank, and Industrial and Commercial Bank of China; directly using the order by field cannot get the order we want
Similarly, for types, we want to sort in the order of repurchase, buy, and sell, and a simple order by field cannot be implemented;
Second, the solution
1: Custom sorting implementation
01. case when conversion implements custom sorting;
Use case when to convert Bank of China, China Construction Bank, and Industrial and Commercial Bank of China into 1, 2, and 3, so that we can use order by to implement custom rearrangement of data.
02: mysql sorting function filed implements custom sorting (recommended);
The field() function in MySQL can be used to sort the query result set in SQL in a specified order.
field(str,str1,str2,str3…) The field str returns the result set of the query in the order of the strings str1, str2, str3….If the str field value in the table does not exist in the records in str1, str2, str3, put it at the top of the result set and return it.
03: The function locate implements custom sorting;
You can also use the locate function to achieve sorting;
locate(subStr,string): determine whether the string (string) contains another string (subStr), the function returns the position where subStr appears in the string;
04: The function lnstr implements custom sorting;
INSTR(STR,SUBSTR) Search for the specified character (SUBSTR) in a string (STR), and return the position where the specified character is found (INDEX);
It is roughly the same as the locate function, but the function has a parameterFirst, one after; 
05: Use the data dictionary to implement custom sorting;
For some relatively fixed fields, data dictionary items can generally be created, which is not only convenient for returning to the front-end for pull-down, etc., but also convenient for the maintenance of later additions and modifications, and can also give the corresponding order in the data dictionary; mainlyIt is convenient for later adjustment and maintenance; it is only necessary to modify the sorting in the data dictionary;
First, it is necessary to establish the corresponding data dictionary items in the data dictionary table and maintain the order;
Secondly, you can directly store the corresponding data dictionary value when storing, and then associate the query and use the data dictionarySort in order; just;
If the order changes,Or to add a new type, you only need to maintain it in the data dictionary, no need to modify sql, and the scalability is better (if necessary, it can even be specially maintained for the configuration page of the data dictionary);


For record only, for future reference, if there is any error, please feel free
Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
猜你喜欢

【云原生】什么是CI/CD? | CI/CD 带来的好处

H5接入支付流程-微信支付&支付宝支付

interrupt()、interrupted()和isInterrupted()你真的懂了吗

MySQL 8.0.29 decompressed version installation tutorial (valid for personal testing)

matlab simulink 模糊pid结合smith控制温度

UE4 事件图表不小心拉了很远,找不到一开始创建的节点

迅为RK3568开发板编译Buildroot-全自动编译

简道云-灵活易用的应用搭建平台

Go语学习笔记 - 处理超时问题 - Context使用 从零开始Go语言

matlab simulink 飞机飞行状态控制
随机推荐
navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)
Js数据类型转化之数组的join方法
MySQL大批量造数据
UE4 事件图表不小心拉了很远,找不到一开始创建的节点
简道云-灵活易用的应用搭建平台
【云原生】什么是CI/CD? | CI/CD 带来的好处
Go语学习笔记 - 处理超时问题 - Context使用 从零开始Go语言
【语义分割】FCN
MySQL 8.0.29 set and modify the default password
UE4 3DUI显示与交互案例
Google Chrome(谷歌浏览器)安装使用
How to quickly delete the compressed package password?
编译失败:HBuilderX 安装目录不能包括 ( 等特殊字符 (HBuilderX,uni-app报错)
PDF file conversion format
[QNX Hypervisor 2.2用户手册]9.20 vdev
mysql实现按照自定义(指定顺序)排序
SQL数据表增加列
MySQL 5.7升级到8.0详细过程
系统(层次)聚类
MySQL夺命10问,你能坚持到第几问?