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"'
相关推荐
这孩子叫逆5 小时前
6. 什么是MySQL的事务?如何在Java中使用Connection接口管理事务?
数据库·mysql
掘根7 小时前
【网络】高级IO——poll版本TCP服务器
网络·数据库·sql·网络协议·tcp/ip·mysql·网络安全
Java小白笔记8 小时前
关于使用Mybatis-Plus 自动填充功能失效问题
spring boot·后端·mybatis
Bear on Toilet8 小时前
初写MySQL四张表:(3/4)
数据库·mysql
无妄啊______8 小时前
mysql笔记9(子查询)
数据库·笔记·mysql
Looooking9 小时前
MySQL 中常用函数使用
数据库·mysql
island13149 小时前
从 InnoDB 到 Memory:MySQL 存储引擎的多样性
数据库·学习·mysql
ZZDICT9 小时前
MySQL 子查询
数据库·mysql
柳鲲鹏10 小时前
编译成功!QT/6.7.2/Creator编译Windows64 MySQL驱动(MinGW版)
开发语言·qt·mysql
一个很帅的帅哥10 小时前
实现浏览器的下拉加载功能(类似知乎)
开发语言·javascript·mysql·mongodb·node.js·vue·express