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"'
相关推荐
JuneXcy12 小时前
第4章 Mysql数据操纵语句--单表查询
mysql
Java程序之猿12 小时前
SpringBoot + camel+IBM MQ实现消息队列处理
java·spring boot·mybatis
白露与泡影13 小时前
MySQL 时间类型选型避坑:timestamp 和 datetime 该怎么选?
数据库·mysql
运维 小白14 小时前
2. 部署mysql服务并监控mysql
数据库·mysql·adb
北漂Zachary15 小时前
Mysql中使用sql语句生成雪花算法Id
sql·mysql·算法
橘颂TA16 小时前
【MySQL】解锁表的 N 种牵手方式:SQL 连接与子查询漫游(复合查询)
数据库·mysql
wyt53142917 小时前
基于人脸识别和 MySQL 的考勤管理系统实现
数据库·mysql
qiuyuyiyang17 小时前
MySQL 实验1:Windows 环境下 MySQL5.5 安装与配置
windows·mysql·adb
青柠代码录18 小时前
【MySQL】SELECT 语句执行流程
数据库·mysql
罗小爬EX18 小时前
MyBatis Interceptor执行顺序详解(plugin机制、责任链模式)
mybatis·interceptor