`
rebeidian
  • 浏览: 24448 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

mysql中find_in_set使用

阅读更多

mysqlfind_in_set使用

 

手册上是这样解释find_in_set的:

 

FIND_IN_SET(str,strlist)

 

Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by
, characters. If the first argument is a constant string and the second is a column of type SET, the FIND_IN_SET() function is optimized to use bit arithmetic. Returns 0 if str is not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL. This function does not work properly if the first argument contains a comma (,
) character.

 

mysql>; SELECT FIND_IN_SET('b','a,b,c,d');

               ->; 2

 

我们在实际中使用这个函数则是由于排序的需要。


例如,有个game表,其中包括字段gameidtitle,现在需要根据一个gameid的集合来获得game表中对应的title字段,可以使用


 

mysql>; select gameid,title from game where gameid in(3,1,2);

 

来获得这样的结果:


+
——–+——————–
+

| gameid | title |

+
——–+——————–
+

| 1 | Young Lee |

| 2 | Dark Matter |

| 3 | Armed With Wings 2 |

+
——–+——————–
+

 

我们会发现,返回结果并不是按照我们给定的gameid集合的顺序来排序的。


如果需要返回结果按(3,1,2)来排序的话,就需要使用find_in_set了。


 

mysql>; select gameid,title from game where gameid in(3,1,2) order by find_in_set

(gameid,'3,1,2');

 

返回的结果如下:


+
——–+——————–
+

| gameid | title |

+
——–+——————–
+

| 3 | Armed With Wings 2 |

| 1 | Young Lee |

| 2 | Dark Matter |

+
——–+——————–+

分享到:
评论

相关推荐

    mysql中find_in_set函数的基本使用方法

    FIND_IN_SET(str,strlist) 定义 假如字符串str在由多个子链组成的字符串列表strlist中,则返回值的范围在1到N之间。 一个字符串列表就是一个由一些被‘,’符号分开的自链组成的字符串。 如果第一个参数是一个...

    PostgreSQL find_in_set 内核开发

    博客:PostgreSQL的学习心得和知识总结(六十五)|关于PostgreSQL数据库 实现MySQL数据库find_in_set()函数 的实现方案

    MySQL的FIND_IN_SET函数使用方法分享

    很多时候我们在设计数据库... 先看mysql手册中find_in_set函数的语法: FIND_IN_SET(str,strlist) 假如字符串str 在由N 子链组成的字符串列表strlist 中,则返回值的范围在 1 到 N 之间。一个字符串列表就是一个由一些被

    Mysql中FIND_IN_SET()和IN区别简析

    主要介绍了Mysql中FIND_IN_SET()和IN区别简析,设计实例代码,具有一定参考价值。需要的朋友可以了解。

    mysql中find_in_set()函数的使用及in()用法详解

    主要介绍了mysql中find_in_set()函数的使用以及in()用法详解,需要的朋友可以参考下

    mysql中find_in_set()函数的使用详解

    首先举个例子来说: 有个文章表里面有个type字段,它存储的是文章类型,有 1头条、2推荐、3热点、4图文等等 。 现在有篇文章他既是头条,又是热点...MySQL手册中find_in_set函数的语法: FIND_IN_SET(str,strlist) str

    Mysql中的find_in_set的使用方法介绍

    使用举例: 用户表(user)中的一个字段(purview)代表该用户的权限 将用户的所有权限id存入此字段。比如有:1,2,3,4,5这样的五... 您可能感兴趣的文章:mysql通过find_in_set()函数实现where in()顺序排序Mysql中FIND_IN_S

    mysql通过find_in_set()函数实现where in()顺序排序

    本文章来为各位介绍一篇关于mysql 实现按 where in () 中的顺序排序,用find_in_set() 函数的教程,希望此教程能够对各位有所帮助。 select * from table where id in ('783',' 769',' 814',' 1577',' 1769') ...

    MySQL高效模糊搜索之内置函数locate instr position find_in_set使用详解

    1.使用locate()方法 普通用法: SELECT`column`from`table`wherelocate('keyword',`condition`)>0 类似于java的indexOf(); 不过locate()只要找到返回的结果都大于0(即使是查询的内容就是最开始部分),没有查找到才...

    MySql逗号拼接字符串查询的两种方法

    下面两个函数的使用和FIND_IN_SET一样,使用时只需要把FIND_IN_SET换成FIND_PART_IN_SET或FIND_ALL_PART_IN_SET 例如某字段里是为1,2,3,4,5  使用方法: 第一种,传入1,3,6 可以查出来 select * from XXX where FIND...

    MySQL Like语句的使用方法

    这样能把所有不含英文的都搞出来 代码如下:SELECT *FROM tableWHERE nameNOT REGEXP ‘[a-zA-Z0-9]+’当然除了regexp之外还可以使用FIND_IN_SET,like来操作FIND_IN_SETmysql中如何使用FIND_IN_SET(),以及使用FIND_...

    使用tp框架和SQL语句查询数据表中的某字段包含某值

    有时我们需要查询某个字段是否包含某值时,通常用like进行模糊查询,但对于一些要求比较准确的查询时(例如:微信公众号的关键字回复匹配查询...以下是在tp框架中使用find_in_set()函数的查询示例: $keyword = '你好';

    sys_area.sql

    2018行政区划数据,报表城市,经纬度,...and find_in_set('360000',`path`); region 区/县 -- 九江 select id,name,lat,lng,pinyin from sys_area where 1=1 and `level` = 3 and find_in_set('360400',`path`);

Global site tag (gtag.js) - Google Analytics