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"'
相关推荐
扑火的小飞蛾7 小时前
生产级SOP:MySQL二进制安装与多实例部署实战(5.6/5.7/8.0/8.4全版本)
数据库·mysql·adb
箓维9 小时前
MySQL_表的约束
android·数据库·mysql
无级程序员10 小时前
dolphinscheduler3.x从mysql迁移到postgresql
数据库·mysql·postgresql
HEJOO911 小时前
MySQL 日期时间类型怎么选:DATE、DATETIME、TIMESTAMP、TIME、YEAR 全解析
android·数据库·mysql
codeejun14 小时前
每日一Go·MySQL-28、账户转账事务设计
开发语言·mysql·golang
箓维14 小时前
MySQL_数据类型
数据库·mysql
千寻技术帮16 小时前
医院挂号小程序(源码+脚本+文档)50031
mysql·微信小程序·小程序·毕业设计·医院管理·医院挂号
程序猿老A17 小时前
轻量应用服务器怎么配置mysql
android·mysql·adb
weixin_3993806917 小时前
TongWeb 数据源报「连接中断」?一个验证超时与 MySQL 驱动兼容的排查记录 by lqw+sy+jjx
数据库·mysql
扑火的小飞蛾1 天前
生产级SOP:MySQL高可用架构与无感知切换实战(主从/MHA/InnoDB Cluster全覆盖)
数据库·mysql·架构