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"'
相关推荐
古月方枘Fry40 分钟前
基于大模型+MySQL的innoai助手(可适配多数环境)
网络·数据库·mysql·aigc
无忧.芙桃2 小时前
MySQL数据库原理与实践(四):基本查询
大数据·数据库·mysql
三8443 小时前
get方法/post方法/SQL注入文字型/数字型
数据库·sql·mysql
啦啦啦啦啦zzzz4 小时前
oat++框架应用之do、dao、service
服务器·c++·mysql·oatpp
暖和_白开水4 小时前
数据分析agent(十三_5):meta_demo:mysql
mysql·数据挖掘·数据分析
CodeStats13 小时前
【Spring事务】Spring事务注解 @Transactional 完整体系:从 MySQL 隔离级别到 MyBatis 原理详解
java·spring·mybatis·事务·transactional
暖和_白开水16 小时前
数据分析agent(十三_3):docker mysql容器镜像服务的异常错误
mysql·docker·容器
ttwuai18 小时前
AI 生成后台改数据后,操作日志别只记按钮:Go + MySQL 怎么验
数据库·人工智能·mysql·golang
韩楚风20 小时前
【参天引擎】一次宕机后的数据恢复,让我把 Cantian 持久化与恢复的六大机制全搞明白了
服务器·网络·数据库·分布式·mysql·架构·cantian
Mico1820 小时前
MySQL 8.0.35 mydumper/myloader 备份恢复工具使用
mysql