当前位置:网站首页>Mysql把查询到的结果集按指定顺寻进行排序
Mysql把查询到的结果集按指定顺寻进行排序
2022-07-29 05:06:00 【nianyuw】
在开发过程中可能会遇到,如何想把查询到的结果集按指定顺序进行排序,就可以使用FIELD函数
- FIELD(str,str1,str2,str3):
其中str是字段名称,可以对SQL查询的结果集按照指定顺序排序,字段str按str1,str2,str3的顺序返回查询到的结果集
案例:查询的结果将推送状态(0:正常 1:优推 2:下架)按优推,正常,下架排序,状态如果相同按更新时间降序
select 查询的数据 from 查询的表 where 查询的条件 order by FIELD(push_status,1,0,2)
在mybatisPlus中可以使用
queryWrapper.last("order by FIELD(push_status,1,0,2");
举例:
SELECT t_enterprise.name,t_enterprise.audit_status FROM t_enterprise WHERE delete_flag = 0 order by audit_status
SELECT t_enterprise.name,t_enterprise.audit_status FROM t_enterprise WHERE delete_flag = 0 order by FIELD(audit_status,1,0,2)
上面两个sql语句都进行了拍寻,一个是默认升序,一个使用了FIELD函数
结果如下图
第一个sql
第二个sql
边栏推荐
- Five correlation analysis, one of the most important skills of data analysts
- Diagram of odoo development tutorial
- stack和queue和优先级队列(大堆和小堆)模拟实现和仿函数讲解
- excel怎么设置行高和列宽?excel设置行高和列宽的方法
- 【文件下载】Easyexcel快速上手
- Lenovo Savior r7000+ add ssd+ copy and partition the information of the original D disk to the new SSD
- Un7.28: common commands of redis client.
- def fasterrcnn_ resnet50_ FPN () instance test
- How is the entered query SQL statement executed?
- How to solve the problem of configuring the progress every time Office2010 is opened?
猜你喜欢

Pivot table of odoo development tutorial

Google gtest事件机制

office2010每次打开都要配置进度怎么解决?

【文件下载】Easyexcel快速上手

What are the core features of the digital transformation of state-owned construction enterprises?

MySQL定时调用预置函数完成数据更新

1 句代码,搞定 ASP.NET Core 绑定多个源到同一个类

如何安装office2010安装包?office2010安装包安装到电脑上的方法

How to solve the problem of configuring the progress every time Office2010 is opened?

Huawei ilearning AI mathematics foundation course notes
随机推荐
[wechat applet] swiper slides the page, and the left and right sides of the slider show part of the front and back, showing part of the front and back
Reply from the Secretary of jindawei: the company is optimistic about the market prospect of NMN products and has launched a series of products
Numpy basic learning
What if the computer cannot open excel? The solution of Excel not opening
力扣------对奇偶下标分别排序
新产品上市最全推广方案
How to debug UDP port
Wps如何使用智能填充快速填充数据?Wps快速填充数据的方法
MySQL regularly calls preset functions to complete data update
Network Security Learning - Intranet Security 1
TCP三次握手四次挥手
ODOO开发教程之透视表
WPS insert hyperlink cannot be opened. What should I do if I prompt "unable to open the specified file"!
The most comprehensive promotion plan for the launch of new products
Office提示系统配置无法运行怎么办?
Simple user-defined authentication interface rules
Box horizontal vertical center layout (summary)
Force deduction ----- sort odd and even subscripts respectively
AttributeError: ‘module‘ object has no attribute ‘create_connection‘
自贸经济中架起的“隐形桥梁”:国货精品与中国AI力量