MySQL 关于 in,find_in_set,locate 多值匹配问题

文章目录

    • 问题场景
    • in 和 find_in_set的区别
    • 有序匹配 locate
    • 无序匹配 Mybatis foreach
    • 我的公众号

问题场景

现在有一张表,type,是逗号拼接存储的。
MySQL 关于 in,find_in_set,locate 多值匹配问题_第1张图片

in 和 find_in_set的区别

SELECT * FROM `test` where type in(1);

SELECT * FROM `test` where FIND_IN_SET(1,type);

MySQL 关于 in,find_in_set,locate 多值匹配问题_第2张图片

有序匹配 locate

SELECT * FROM `test` where LOCATE('1,3',type);

MySQL 关于 in,find_in_set,locate 多值匹配问题_第3张图片

无序匹配 Mybatis foreach

<select id="find" resultMap="resultMap" parameterType="object">
		SELECT * FROM `test` where 1=1 and
		<foreach item="item" collection="typeList" separator="OR" open="(" close=")" index="">
			find_in_set(#{item},type)
		foreach>
	select>
# 包含1和3的都查出来了
SELECT * FROM `test` where 1=1 and ( FIND_IN_SET(1,type) OR FIND_IN_SET(3,type) )

MySQL 关于 in,find_in_set,locate 多值匹配问题_第4张图片

我的公众号

程序员进化之路,从一个简单的公众号开始

你可能感兴趣的:(MySQL)