mysql regexp匹配多個字符串實現(xiàn)
更新時間:2024年09月18日 11:23:40 作者:濤哥是個大帥比
本文主要介紹了mysql regexp匹配多個字符串實現(xiàn),可以利用REGEXP正則表達式匹配多個字符串,從而實現(xiàn)高效查詢,具有一定的參考價值,感興趣的可以了解一下
項目場景:
數(shù)據(jù)結構

其中nameArr存儲的是名字集合,現(xiàn)在的需求是傳入"aaa","fff",需要把包含這兩個name的數(shù)據(jù)都查出來。
解決方案:
可以使用
REGEXP來匹配包含多個特定ID的字符串。使用以下正則表達式:
select * from test where nameArr regexp '"aaa"|"fff"'
使用mybatis實現(xiàn)
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這個集合會循環(huán)逗號拼接,打印sql
select * from test
where nameArr regexp concat('"',concat_ws('"|"','aaa','fff'),'"')最終的sql
select * from test where nameArr regexp '"aaa"|"fff"'
到此這篇關于mysql regexp匹配多個字符串實現(xiàn)的文章就介紹到這了,更多相關mysql regexp匹配多個字符串內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

