MySQL中的field()函数,可以用来对SQL中查询结果集进行指定顺序排序。

函数使用格式如下:

order by (str,str1,str2,str3,str4……),str与str1,str2,str3,str4比较,其中str指的是字段名字,

意为:字段str按照字符串str1,str2,str3,str4的顺序返回查询到的结果集。如果表中str字段值不存在于str1,str2,str3,str4中的记录,放在结果集最前面返回。

例:

表数据如下:

root@localhost|iris>select * from ta;
+----+--------+------+-------+
| id | name   | age  | class |
+----+--------+------+-------+
|  1 | iris   |   11 | a1    |
|  2 | iris   |   22 | a2    |
|  3 | seiki  |   33 | a3    |
|  4 | seiki  |   44 | a4    |
|  5 | xuding |   55 | a5    |
|  6 | xut    |   66 | a6    |
|  7 | iris   |   12 | a2    |
|  8 | iris   |   24 | a4    |
|  9 | seiki  |   36 | a6    |
| 10 | seiki  |   48 | a8    |
| 11 | xuding |   50 | a0    |
| 12 | xut    |   77 | a7    |
+----+--------+------+-------+
12 rows in set (0.00 sec)
按照'seiki','iris','xut'来排序,结果如下:

root@localhost|iris>select * from ta order by field(name,'seiki','iris','xut');
+----+--------+------+-------+
| id | name   | age  | class |
+----+--------+------+-------+#不在str1,str2,str3中的内容,放在最前面返回,str值相同按照主键的顺序
|  5 | xuding |   55 | a5    |
| 11 | xuding |   50 | a0    |
|  3 | seiki  |   33 | a3    |
|  4 | seiki  |   44 | a4    |
|  9 | seiki  |   36 | a6    |
| 10 | seiki  |   48 | a8    |
|  1 | iris   |   11 | a1    |
|  2 | iris   |   22 | a2    |
|  7 | iris   |   12 | a2    |
|  8 | iris   |   24 | a4    |
|  6 | xut    |   66 | a6    |
| 12 | xut    |   77 | a7    |
+----+--------+------+-------+
12 rows in set (0.00 sec)
按照'seiki','iris'来排序,结果如下:

root@localhost|iris>select * from ta order by field(name,'seiki','iris');
+----+--------+------+-------+
| id | name   | age  | class |
+----+--------+------+-------+#不在str1,str2,str3中的内容,放在最前面返回;str值相同按主键顺序排列
|  5 | xuding |   55 | a5    |
|  6 | xut    |   66 | a6    |
| 11 | xuding |   50 | a0    |
| 12 | xut    |   77 | a7    |
|  3 | seiki  |   33 | a3    |
|  4 | seiki  |   44 | a4    |
|  9 | seiki  |   36 | a6    |
| 10 | seiki  |   48 | a8    |
|  1 | iris   |   11 | a1    |
|  2 | iris   |   22 | a2    |
|  7 | iris   |   12 | a2    |
|  8 | iris   |   24 | a4    |
+----+--------+------+-------+
12 rows in set (0.00 sec)
按照'seiki','iris' desc 来排序,结果如下:

root@localhost|iris>select * from ta order by field(name,'seiki','iris') desc;
+----+--------+------+-------+
| id | name   | age  | class |
+----+--------+------+-------+
|  1 | iris   |   11 | a1    |
|  2 | iris   |   22 | a2    |
|  7 | iris   |   12 | a2    |
|  8 | iris   |   24 | a4    |
|  3 | seiki  |   33 | a3    |
|  4 | seiki  |   44 | a4    |
|  9 | seiki  |   36 | a6    |
| 10 | seiki  |   48 | a8    |#倒序时,按照str3,str2,str1的顺序逆向排序,
|  5 | xuding |   55 | a5    |#不在str1,str2,str3中的记录放到最后;str值相同按主键顺序排列
|  6 | xut    |   66 | a6    |
| 11 | xuding |   50 | a0    |
| 12 | xut    |   77 | a7    |
+----+--------+------+-------+

MySQL中函数field()的用法相关推荐

  1. MySQL中sum和count用法总结

    MySQL中sum和count用法总结 1.sum 2.count 例子:要求:查询出2门及2门以上不及格者的平均成绩. 3.MySQL 分组之后如何统计记录条数 gourp by 之后的 count ...

  2. Mysql coalesce()函数认识和用法

    Mysql coalesce()函数认识和用法 coalesce()解释:返回参数中的第一个非空表达式(从左向右):  鉴于在 mysql中没有nvl()函数, 我们用coalesce()来代替.   ...

  3. Mysql中where和having用法及区别

    1.Mysql中Having和Where用法 当SQL语句中使用聚合函数,然后又需要对聚合函数的记录集进行筛选过滤时,这个时候就需要用到Having子句.如果是在聚合函数前过滤记录用Where,在聚合 ...

  4. 16、mysql中函数的创建和使用

    sql语言可以进行编程,说到编程,那一定少不了使用函数对代码进行最小级别的封装,接下来介绍一下mysql中函数的写法以及应用 mysql中的函数可分为两类:系统函数.用户自定义函数. 简单的介绍几个常 ...

  5. MySQL中ESCAPE关键字的用法详解

    MySQL中ESCAPE关键字的用法详解 MySQL中ESCAPE关键字的用法详解 1. mysql转义概述 2. escape验证 MySQL中ESCAPE关键字的用法详解 1. mysql转义概述 ...

  6. mysql中函数mid_MySQL MID()函数的用法详解(代码示例)

    在MySQL中,MID()函数返回从指定位置开始的子字符串. MID()和SUBSTR()都是SUBSTRING()的同义词. 基本语法是这样的:MID(str,pos,len) 这里,str是字符串 ...

  7. mysql substring_index_mysql函数substring_index的用法

    MySQL中一个很好用的截取字符串的函数:substring_index. 用法规则: substring_index("待截取有用部分的字符串","截取数据依据的字符& ...

  8. MySQL中函数CONCAT及GROUP_CONCAT

    一.CONCAT()函数 CONCAT()函数用于将多个字符串连接成一个字符串. 使用数据表Info作为示例,其中SELECT id,name FROM info LIMIT 1;的返回结果为 +-- ...

  9. MYSQL中replace into的用法

    今天在编程的时候,学习了replace into的用法,真的很好用,是insert into的增强版.在向表中插入数据时,我们经常会遇到这样的情况:1.首先判断数据是否存在:2.如果不存在,则插入:3 ...

最新文章

  1. 高并发场景下,到底先更新缓存还是先更新数据库?
  2. mysql img格式缩放,再学 GDI+[89]: TGPImage(9) - 图像缩放时的质量(算法)
  3. 进化深度神经网络是推动技术发展的强劲动力
  4. JNI开发篇——报错:Flag android.useDeprecatedNdk is no longer supported and will be removed in the next……...
  5. mysql大量数据插入探讨(量变引起质变)
  6. boost::process::extend相关的测试程序
  7. redis linux无法启动服务,CentOS7 下redis不能开机启动,求解?
  8. 深入理解vue中的slot与slot-scope
  9. 开源重量级的流程引擎或UI引擎
  10. [渝粤教育] 中国地质大学 结构力学 复习题
  11. 【大数据-第二期】java基础第五天作业
  12. echarts实现航线图,航运图
  13. 笛卡尔的爱心函数Java语言_笛卡尔情书的秘密——心形函数的绘制
  14. html如何退出登录,微信小程序怎么退出登录
  15. 主题:讲解三层代码讲解--第三课(*****) DATE:2004-05-28
  16. 编译原理实验四 C-语言 语法分析程序
  17. 大数据之初识Doris
  18. oracle查看用户 同义词,oracle同义词
  19. 大学生月入过万的创业好项目——自媒体
  20. 【APUE笔记】第十二章 高级I/O

热门文章

  1. Codeforces 1247D. Power Products (logn质因数分解)
  2. OTN告警测试1:LOS
  3. 用8051单片机编程由P1.0和P1.1引脚分别输出周期为1ms和500us的方波
  4. windows 7 浏览器无法进网站,提示安全证书存在问题(GlobalSign)
  5. [2011JMP]Some Serrin-type regularity criteria for weak solutions to the Navier-Stokes equations
  6. 边云协同的优点_云边协同的现实意义
  7. 基于STM32F03RCT的多路频率占空比可调PWM输出
  8. Redis-学习笔记整理+汇总
  9. 小米Mix2s刷机教程
  10. python中numpy函数ftt_语音MFCC提取:librosa python_speech_feature(2019.12)