#### 1. find_in_set() 用于在多个字符串子链中查询字符串 --- ```sql find_in_set(str, strlist) ``` str: 要查询的字符串 strlist: 字段名或字符串, 多个子链以英文逗号 `分割 返回值: 假如字符串 str 在由 N 个子链组成的字符串列表 strlist 中,则返回值的范围在 1 到 N 之间, 不在 strlist 中则返回 0 以下示例中返回值则为: 3 , 因为字符串 **yang** 在第三个子链中 ```sql select find_in_set('yang', 'liang,chen,yang'); ``` 应用场景: 在文章表 article 中有个标签字段 tags,一个文章可以有多个标签 标签 id: 1 html 2 css 3 javascript, tags 以 1,2,3 的格式存储标签,那么我们可以使用 find_in_set 查找出 tags 中有 1 的标签 ```sql select * from article where find_in_set(1, `tags`); ``` 在 ThinkPHP 6 中可以使用查询表达式(TP6中的新增的,之前版本不可用) ``` User::whereFindInSet('shop_ids', 1)->select(); ``` 生成的SQL语句为 ```sql SELECT * FROM `user` WHERE FIND_IN_SET(1, `shop_ids`); ``` #### 2. instr() 用于在字符串中查询子字符串 --- 用于在字符串中查询子字符串, 返回子字符串在字符串中首次出现的位置(下标从 1 开始);如果没有找到,则返回 0 ```sql select * from `article` where instr(`tags`, 2); select * from `article` where `tags` like '%2%'; ```