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"'
相关推荐
暮雨疏桐1 小时前
MySQL SQL Mode及其说明
数据库·sql·mysql·sql mode
Tangcan-1 小时前
【MySQL】数据库基础
数据库·mysql
蔡蓝1 小时前
Mysql的索引,慢查询和数据库表的设计以及乐观锁和悲观锁
数据库·mysql
钢铁男儿3 小时前
C# 方法(可选参数)
数据库·mysql·c#
陆少枫3 小时前
MySQL基础关键_013_常用 DBA 命令
数据库·mysql
Johny_Zhao3 小时前
K8S+nginx+MYSQL+TOMCAT高可用架构企业自建网站
linux·网络·mysql·nginx·网络安全·信息安全·tomcat·云计算·shell·yum源·系统运维·itsm
阿乾之铭4 小时前
Spring Boot 参数验证
java·数据库·mysql
唐人街都是苦瓜脸5 小时前
MySQL创建了一个索引表,如何来验证这个索引表是否使用了呢?
数据库·mysql
zhcong_5 小时前
MySQL数据库操作
数据库·mysql
悟空打码7 小时前
MyBatis源码解读5(3.1、缓存简介)
缓存·mybatis