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"'
相关推荐
影子24014 小时前
Navicat导出mysql数据库表结构说明到excel、word,单表导出方式记录
数据库·mysql·excel
潘多编程6 小时前
SpringBoot分布式项目订单管理实战:Mybatis最佳实践全解
spring boot·分布式·mybatis
要天天开心啊7 小时前
MyBatis第二天笔记
笔记·tomcat·mybatis
开发小能手-roy9 小时前
ubuntu 安装mysql
mysql·ubuntu·adb
是阿建吖!10 小时前
【MySQL】事务
数据库·mysql
Asuka0710 小时前
MySQL数据库和表的操作
数据库·mysql
暗恋 懒羊羊11 小时前
【MySQL】表的操作
数据库·mysql
于过12 小时前
基于Mybatis的SQL模版解析
后端·mybatis
小刘|14 小时前
Mybatis_Plus中的常用注解
java·spring·mybatis
弈风千秋万古愁15 小时前
python 语法篇(一)
数据库·python·mysql