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"'
相关推荐
爆更小哇9 小时前
MyBatis的TypeHandler :优雅地实现数据加密与解密
数据库·后端·mybatis
凌寒1110 小时前
Linux(Debian)安装、卸载 MySQL
linux·运维·mysql·debian
oneslide11 小时前
分享一个MySQL数据库备份恢复脚本--II
数据库·mysql
q***239212 小时前
MySQL数据库误删恢复_mysql 数据 误删
数据库·mysql·adb
合作小小程序员小小店12 小时前
web网页开发,在线%图书管理%系统,基于Idea,html,css,jQuery,java,ssm,mysql。
java·前端·后端·mysql·jdk·intellij-idea
IUGEI12 小时前
【MySQL】SQL慢查询如何排查?从慢查询排查到最终优化完整流程
java·数据库·后端·mysql·go
合作小小程序员小小店12 小时前
web网页开发,在线%食堂管理%系统,基于Idea,html,css,jQuery,java,ssm,mysql。
java·前端·mysql·html·intellij-idea·jquery
w***48113 小时前
Springboot项目本地连接并操作MySQL数据库
数据库·spring boot·mysql
友友马13 小时前
『MySQL』 - 事务 (二)
数据库·mysql·oracle
java1234_小锋14 小时前
讲讲Mybatis的一级、二级缓存?
java·开发语言·mybatis