当前位置:网站首页>MySql指定字段排序、自定义排序位置
MySql指定字段排序、自定义排序位置
2022-04-22 21:06:00 【码奴生来只知道前进~】
目录
1、创建测试表和添加数据
/*
Navicat Premium Data Transfer
Source Server : test
Source Server Type : MySQL
Source Server Version : 50725
Source Host : localhost:3306
Source Schema : test
Target Server Type : MySQL
Target Server Version : 50725
File Encoding : 65001
Date: 22/04/2022 14:24:01
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for field_data
-- ----------------------------
DROP TABLE IF EXISTS `field_data`;
CREATE TABLE `field_data` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '名称',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of field_data
-- ----------------------------
INSERT INTO `field_data` VALUES (1, '第1条数据');
INSERT INTO `field_data` VALUES (2, '第2条数据');
INSERT INTO `field_data` VALUES (3, '第3条数据');
INSERT INTO `field_data` VALUES (4, '第4条数据');
INSERT INTO `field_data` VALUES (5, '第5条数据');
INSERT INTO `field_data` VALUES (6, '第6条数据');
SET FOREIGN_KEY_CHECKS = 1;
2、一般的排序写法
SELECT
*
FROM
`field_data`
ORDER BY
id DESC,
NAME ASC;
3、通过mysql的FIELD()函数指定排序
3.1定义和用法
FIELD()函数返回值列表中值的索引位置。此函数执行不区分大小写的搜索。
注意:如果在值列表中找不到指定的值,则此函数将返回0.如果value为NULL,则此函数将返回0。
3.2 语法
FIELD(value, val1, val2, val3, ...)
3.3 参数值
| 参数 | 描述 |
|---|---|
| value | 必须。要在列表中搜索的值 |
| val1,val2,val3,.... | 必须。要搜索的值列表 |
3.4 实际写法demo
SELECT
*
FROM
`field_data`
ORDER BY
FIELD( id, 3, 2, 4 ) DESC,
FIELD( NAME, "第5条数据" ) ASC;

版权声明
本文为[码奴生来只知道前进~]所创,转载请带上原文链接,感谢
https://blog.csdn.net/tanqingfu1/article/details/124344979
边栏推荐
- JMeter complete set of data
- 基于PAOGD的角色动画基础设计
- 基于OpenGL的贪吃蛇游戏设计与实现
- 基于PAOGD_HW1的弹出的小球-简单建模、插值动画
- 1、 Machine learning concepts
- 2022 G3 boiler water treatment national question bank and online simulation examination
- DSPACE仿真平台的使用
- Handwritten linked list ~ contains one-way linked list and two-way linked list. Please rest assured
- UnityShader入门精要——素描效果渲染
- Consul 的使用
猜你喜欢

Application value of smart agriculture app software
![[200 opencv routines of youcans] 160 Otsu method of image processing](/img/f1/8977310721848e68a5d03faf0e8ae4.png)
[200 opencv routines of youcans] 160 Otsu method of image processing

nodejs笔记4

1、 Machine learning concepts

MySQL 进阶 存储过程 存储函数 -- 存储过程介绍、存储过程基本语法、变量(系统变量、用户定义变量、局部变量)、if、参数、case、while、repeat、loop、游标、条件处理程序

Lizard book learning Day1 - overview of machine learning

LeetCode HOT 100(买卖股票的最佳时机)

Leetcode question brushing Diary - Sword finger offer II 070 Sort numbers that appear only once in the array

【youcans 的 OpenCV 例程200篇】160. 图像处理之OTSU 方法

智慧农业APP软件应用价值
随机推荐
Leetcode学习计划之动态规划入门day1,2(共4题)
UnityShader入门精要——素描效果渲染
Nodejs notes 4
Beyond compare enforces the use of binary transfer to ensure that the files are the same
Consul 的使用
Confidence interval and interval estimation
互联网时代没有囊括进来的流量,在产业互联网时代全部都被囊括进来
2022年G3锅炉水处理国家题库及在线模拟考试
Win10 installation neo4j
Lenovo computer housekeeper graphic introduction: how to download Lenovo computer housekeeper?
Leetcode question brushing Diary - Sword finger offer II 070 Sort numbers that appear only once in the array
Leetcode-92-reverse linked list
C # convert ofd to PDF
Jingdong new product Intelligence Bureau spoilers oppo K10 series or linkage Guoman IP surprise?
.bashrc .bash_profile .bash_logout .bash_history
解数独[Pre-DS-hash结构 + component-DFS | Pre-hash-1-9特性--二进制状态压缩 + DFS]
Leetcode Hot 100
软件测试工具最全的抓包工具的综合对比
Knowledge of machine vision
Question bank and simulation examination of work permit for safety management personnel of hazardous chemical business units in 2022