mysql對(duì)以逗號(hào)分隔的字段內(nèi)容進(jìn)行查詢
find_in_set函數(shù)
背景
使用mysql時(shí),有可能一個(gè)字段代表一個(gè)集合,如果將這個(gè)集合單獨(dú)抽成一張表又不值當(dāng)?shù)?,這個(gè)時(shí)候我們存儲(chǔ)時(shí),可以選擇用逗號(hào)將數(shù)據(jù)分隔開(kāi)(只能用英文的逗號(hào)),如圖所示:
做查詢時(shí)怎么查呢?
單條件查詢
假如說(shuō)給一個(gè)數(shù)據(jù)作為查詢條件,判斷該字段是否存在,應(yīng)該怎么查呢?
SELECT * FROM student where find_in_set('唱歌', sign) > 0;
使用find_in_set()函數(shù)輕松實(shí)現(xiàn),將sign字段中含有’唱歌’屬性的數(shù)據(jù)查詢出來(lái),而不是用like。
多條件查詢用于mybatis
1、多個(gè)條件查詢,比如:既符合 唱歌 、又符合 跳舞 的,就可以這樣寫(xiě):
<if test="list!= null and list.size() > 0">
<foreach item="item" index="index" collection="sign.split(',')">
AND find_in_set(#{item} , sign) > 0
</foreach>
</if>
2、多個(gè)條件查詢,比如:符合 唱歌 、或者符合 跳舞 的,就可以這樣寫(xiě):
第一種:
<if test="list!= null and list.size() > 0">
<trim prefix="and (" prefixOverrides="AND|OR" suffix=")">
<foreach collection="list" item="item">
OR find_in_set(#{item} , sign) > 0
</foreach>
</trim>
</if>
第二種:
<if test="list!= null and list.size() > 0">
and
<foreach item="item" index="index" collection="list open="(" separator="or" close=")">
find_in_set(#{item} , sign) > 0
</foreach>
</if>
聚合查詢count總數(shù)
怎么計(jì)算總數(shù)呢?
SELECT sum(LENGTH(sign) - LENGTH(REPLACE(sign,',','')) + 1) count FROM student;
注:原始字段內(nèi)容的長(zhǎng)度 - 把逗號(hào)進(jìn)行刪除后的內(nèi)容長(zhǎng)度 = 該字段中有多少個(gè)逗號(hào);然而最后一位是不帶逗號(hào)的所以要+1。
查詢distinct的列表
沒(méi)有什么更好的辦法,只能先distinct sign這個(gè)字段,查詢出來(lái),然后使用程序挨個(gè)判斷了。。。
find_in_set()函數(shù)走索引嗎
我們使用執(zhí)行計(jì)劃看一下(這里將sign字段設(shè)置為了索引)
locate函數(shù)
一個(gè)字段以逗號(hào)相隔,當(dāng)查詢的時(shí)候,入?yún)⑼菃蝹€(gè)或者集合(單個(gè)使用like或find_in_set)而list時(shí)可以采用以下方法:
select count(*) from engine_temp_variable where del_flag =0 and
<if test="list!= null and list.size() > 0">
<trim prefix="and (" prefixOverrides="AND|OR" suffix=")">
<foreach collection="list" item="id">
or (locate(concat(#{id},','),variableFieldIds) > 0 )
</foreach>
</trim>
</if>
擴(kuò)展:
1、locate(substr,str):返回字符串substr中第一次出現(xiàn)子字符串的位置 str,沒(méi)有出現(xiàn)返回0。
2、concat(str1, str2,…):將多個(gè)字符串拼接為一個(gè),如果有任何一個(gè)參數(shù)為null,則返回值為null。
position函數(shù)
POSITION(substr IN str)
其中substr表示要查找的字符串,str表示被查找的字符串。
因?yàn)閕n標(biāo)識(shí)包含,意為:根據(jù)str這個(gè)條件,篩選substr符合的數(shù)據(jù)
例:
select user_name from sys_user where POSITION(user_name in 'cyadmin')
若是關(guān)聯(lián)表數(shù)據(jù)量過(guò)大時(shí),可以判斷l(xiāng)eft join 傳參
例:
<if test="userId != null and userId != 0"> left join sys_user su on su.user_id = #{userId} and POSITION(r.park_code in su.parks)</if>
入?yún)⑹莑ist,查詢文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-791599.html
<if test="params.spaceTypesList != null and params.spaceTypesList.size() >0">
and
<foreach collection="params.spaceTypesList" item="spaceTypesItem" index="index" open="(" close=")"
separator="or">
POSITION(#{spaceTypesItem} IN space_types)
</foreach>
</if>
參考鏈接:find_in_set()函數(shù)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-791599.html
到了這里,關(guān)于mysql對(duì)以逗號(hào)分隔的字段內(nèi)容進(jìn)行查詢——find_in_set函數(shù)或locate函數(shù)或position函數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!