项目场景:
数据结构
其中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匹配多个字符串内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论