mysql 函数: find_in_set()、instr()
2024-09-20
2
1. find_in_set() 用于在多个字符串子链中查询字符串
find_in_set(str, strlist)
str: 要查询的字符串
strlist: 字段名或字符串, 多个子链以英文逗号 `分割
返回值: 假如字符串 str 在由 N 个子链组成的字符串列表 strlist 中,则返回值的范围在 1 到 N 之间, 不在 strlist 中则返回 0
以下示例中返回值则为: 3 , 因为字符串 yang 在第三个子链中
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 的标签
select * from article where find_in_set(1, `tags`);
2. instr() 用于在字符串中查询子字符串
用于在字符串中查询子字符串, 返回子字符串在字符串中首次出现的位置(下标从 1 开始);如果没有找到,则返回 0
更新于:2小时前select * from `article` where instr(`tags`, 2);
select * from `article` where `tags` like '%2%';
赞一波!
相关文章
- ES6 箭头函数 Arrow Function
- PHP 函数传值和传引用的区别
- PHP 可变函数
- 解释函数 foo() {} 和 var foo = function() {} 之间 foo 用法的差异
- JavaScript 常用自定义功能函数
- PHP 常用功能函数
- PHP的数组排序函数
- MySQL 添加数据 insert 命令及优化
- 修改 MySQL 用户密码
- MySQL 数据库备份和还原数据库 mysqldump、source
- TP6.0 助手函数 env()
- PHP的七个数组指针函数
- Vue组件的data必须是一个函数、单个根元素、局部组件
- 使用docker搭建Nginx + PHP + mysql环境
- PHP 创建目录函数封装
- Navicat Premium 和 Navicat for MySQL 的区别
- ThinkPHP6.0 公共函数文件
- json python中的转储函数
- 地图函数在 Python 中有什么用?
- .NET EF连接MySQL数据库
文章评论
全部评论