天天看點

MySQL 關于 in,find_in_set,locate 多值比對問題

文章目錄

    • 問題場景
    • in 和 find_in_set的差別
    • 有序比對 locate
    • 無序比對 Mybatis foreach
    • 我的公衆号

問題場景

現在有一張表,type,是逗号拼接存儲的。
MySQL 關于 in,find_in_set,locate 多值比對問題

in 和 find_in_set的差別

MySQL 關于 in,find_in_set,locate 多值比對問題
MySQL 關于 in,find_in_set,locate 多值比對問題

有序比對 locate

MySQL 關于 in,find_in_set,locate 多值比對問題

無序比對 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 多值比對問題

我的公衆号

程式員進化之路,從一個簡單的公衆号開始
MySQL 關于 in,find_in_set,locate 多值比對問題

繼續閱讀