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"'
相关推荐
Lisonseekpan1 小时前
MVCC的底层实现原理是什么?
java·数据库·后端·mysql
HMBBLOVEPDX2 小时前
MySQL的存储引擎:
数据库·mysql·存储引擎
灰原喜欢柯南2 小时前
实战:MyBatis 中 db.properties 的正确配置与最佳实践
java·数据库·mybatis
Icey_World2 小时前
Mysql笔记-系统变量\用户变量管理
mysql·存储过程·系统变量·用户变量·会话变量
Starry_hello world2 小时前
MySql 表的操作
数据库·笔记·mysql
秋难降8 小时前
零基础学习SQL(三)——数据查询语言(DQL)
数据库·sql·mysql
tanxiaomi8 小时前
✨ 基于 JsonSerialize 实现接口返回数据的智能枚举转换(优雅告别前端硬编码!)
java·前端·spring·spring cloud·mybatis
22:30Plane-Moon9 小时前
创建MyBatis-Plus版的后端查询项目
mybatis
bthdnj10 小时前
mysql的InnoDB索引总结
数据库·mysql
IT小番茄14 小时前
解决 MySQL 查询速度缓慢的问题
mysql