mysql regexp匹配多个字符串

项目场景:

数据结构

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


解决方案:

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

sql 复制代码
select * from test
where nameArr regexp '"aaa"|"fff"'

使用mybatis实现

mapper

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

xml

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

sql 复制代码
select * from test
where nameArr regexp concat('"',concat_ws('"|"','aaa','fff'),'"')

最终的sql

sql 复制代码
select * from test
where nameArr regexp '"aaa"|"fff"'
相关推荐
Wx-bishekaifayuan1 小时前
基于微信小程序的社区图书共享平台设计与实现 计算机毕业设计源码44991
javascript·vue.js·windows·mysql·pycharm·tomcat·php
哎呀呦呵2 小时前
python内置模块-re模块介绍使用
java·python·mysql
ss2732 小时前
手写MyBatis第104弹:SqlSession从工厂构建到执行器选择的深度剖析
java·开发语言·后端·mybatis
wuyunhang1234563 小时前
MySQL----触发器
数据库·mysql
鸽鸽程序猿4 小时前
【MySQL】索引
数据库·mysql
川石课堂软件测试6 小时前
全链路Controller压测负载均衡
android·运维·开发语言·python·mysql·adb·负载均衡
hjbf7 小时前
理解并解决 MySQL 中的 "You can't specify target table for update in FROM clause" 错误
mysql
一路向北_Coding7 小时前
MyBatis Generator让你优雅的写SQL
mysql·mybatis
程序猿(雷霆之王)9 小时前
MySQL——复合查询
数据库·mysql
莫陌尛.9 小时前
SSM(Spring+SpringMVC+Mybatis)整合
java·spring·mybatis