当前位置: 首页 > news >正文

mysql regexp匹配多个字符串

项目场景:

数据结构

其中nameArr存储的是名字集合,现在的需求是传入"aaa","fff",需要把包含这两个name的数据都查出来。


解决方案:

可以使用REGEXP来匹配包含多个特定ID的字符串。使用以下正则表达式:

select * from test
where nameArr regexp '"aaa"|"fff"'

使用mybatis实现

mapper

/*** 正则匹配多个id字符串*/
List<TestEntity> list(@Param("ids") List<String> ids);

xml

<select id="list" resultType="com.test.TestEntity">select * from test<if test="ids != null and ids.size()>0">and nameArr regexp concat('"',concat_ws('"|"',<foreach collection="ids" item="item" separator=",">#{item}</foreach>),'"')</if>
</select>

解析一下这个sql

ids这个集合会循环逗号拼接,打印sql

select * from test
where nameArr regexp concat('"',concat_ws('"|"','aaa','fff'),'"')

最终的sql

select * from test
where nameArr regexp '"aaa"|"fff"'


http://www.mrgr.cn/news/6618.html

相关文章:

  • 【Qt笔记】QPushButton控件详解
  • 游戏如何对抗 IL2cppDumper逆向分析
  • Centos7配置代理安装最新版Docker并拉取镜像
  • react 中的useState useEffect
  • MySQL用户与权限管理
  • .Net 6 WebApi项目中使用Log4Net详解
  • 单一职责原则在微服务中的应用:服务分解与职责明确
  • 产品需求文档
  • 【C++】提高 -- 类模板
  • C++初学(14)
  • 商品视频与多媒体展示:API返回值中的新趋势
  • 初识 OpenCV:计算机视觉的入门指南
  • 案例:LVS-DR模式
  • 【文献解析】一种像素级的激光雷达相机配准方法
  • python画图高斯平滑均值曲线
  • Python 爬虫入门(十二):正则表达式「详细介绍」
  • 【笔记篇】Davinci Configurator SomeIpXf模块
  • Linux平台Display Server与Desktop Environment
  • 服务路由的使用场景
  • OS-Copilot:自学习的通用计算机智能体