欢迎投稿

今日深度:

mysqlregexp匹配多个字符串实现,

mysqlregexp匹配多个字符串实现,


目录
  • 项目场景:
  • 解决方案:
    • 使用mybatis实现

项目场景:

数据结构

其中nameArr存储的是名字集合,现在的需求是传入"aaa","fff",需要把包含这两个name的数据都查出来。

解决方案:

可以使用REGEXP来匹配包含多个特定ID的字符串。使用以下正则表达式:

select * from test
where nameArr regexp '"aaa"|"fff"'

使用mybatis实现

mapper

/**
 * 正则匹配多个id字符串
 */
List<TestEntity> list(@Param("ids") List<String> ids);

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

select * from test
where nameArr regexp concat('"',concat_ws('"|"','aaa','fff'),'"')

最终的sql

select * from test
where nameArr regexp '"aaa"|"fff"'

到此这篇关于mysql regexp匹配多个字符串实现的文章就介绍到这了,更多相关mysql regexp匹配多个字符串内容请搜索PHP之友以前的文章或继续浏览下面的相关文章希望大家以后多多支持PHP之友! 

您可能感兴趣的文章:
  • 老生常谈MYSQL模式匹配 REGEXP和like的用法
  • MySql比较运算符正则式匹配REGEXP的详细使用详解

www.htsjk.Com true http://www.htsjk.com/Mysql/48338.html NewsArticle mysqlregexp匹配多个字符串实现, 目录 项目场景: 解决方案: 使用mybatis实现 项目场景: 数据结构 其中nameArr存储的是名字集合,现在的需求是传入aaa,fff,需要把包含这两个name的数据都...
评论暂时关闭