Mysql的instr()函數(shù)用法及說明
更新時(shí)間:2026年01月04日 09:12:02 作者:TTc_
mysql中instr()函數(shù)用于返回子字符串在字符串中第一次出現(xiàn)的位置,可以實(shí)現(xiàn)模糊查詢,類似于like用法
Mysql的instr()函數(shù)用法
mysql的內(nèi)置函數(shù)instr(filed,str)
作用是返回str子字符串在filed字符串的第一次出現(xiàn)的位置。
當(dāng)instr(filed,str)=0時(shí)
表示子符串str不存在于字符串filed中,因此可以用來實(shí)現(xiàn)mysql中的模糊查詢,與like用法類似。
如下:
// 1、instr()函數(shù),#{name}為參數(shù)
select id,name from test where instr(name,#{name})>0
上述寫法等同于下面
// 2、like
select id,name from test where name like concat('%',#{name},'%')
instr(filed,str) > 0 ? file like '%str%'
instr(filed,str) = 1 ? file like 'str%'
instr(filed,str) = 0 ? file not like '%str%'下面是一段mapper.xml的部分示例代碼
<select id="selectUserListByConds" parameterType="java.lang.String" resultMap="BaseResultMap">
select
a.userid,
a.username,
a.account,
a.password,
a.mobile,
a.description,
a.delete_flag,
a.enabled,
a.email,
a.address,
a.is_online,
a.created_time,
a.updated_time,
a.created_user,
a.updated_user,
a.org_code,
s.org_name,
a.limit_ip,
a.expiry_date,
a.last_login_time,
a.last_login_ip,
a.account_type,
a.account_type_name,
a.tenant_id
from s_user a
left join s_department s on s.org_code=a.org_code and s.tenant_id=a.tenant_id
where 1=1
<if test="showdelete==null">
and a.delete_flag!=1
</if>
<if test="account!=null and account!=''">
and instr(a.account,#{account})>0
</if>
<if test ="username!=null and username!=''">
and instr(a.username,#{username})>0
</if>
<if test="roleIds!=null and roleIds!=''">
and exists(select * from s_user_role where user_id=a.userid and role_id = #{roleIds})
</if>
<if test="orgCode!=null and orgCode!=''">
and instr(a.org_code,#{orgCode})>0
</if>
<if test="tenantId!=null and tenantId!=''">
and a.tenant_id=#{tenantId}
</if>
order by a.created_time DESC
</select>總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
mysql授予用戶遠(yuǎn)程訪問權(quán)限的實(shí)現(xiàn)
在默認(rèn)情況下,MySQL 數(shù)據(jù)庫僅允許在本地主機(jī)上進(jìn)行訪問,如果您需要遠(yuǎn)程連接到 MySQL 數(shù)據(jù)庫,您需要授予用戶遠(yuǎn)程訪問權(quán)限,本文就來2023-11-11
MySQL檢查和清理不可見字符的實(shí)現(xiàn)步驟
在導(dǎo)入Excel表格到MySQL時(shí),由于未進(jìn)行數(shù)據(jù)清理,導(dǎo)致字段a出現(xiàn)了大量不可見字符的問題,這些字符包括空格、制表符、換行符和BOM頭,下面就來介紹一下如何清理這些字符,感興趣的可以了解一下2026-02-02
MySQL 索引優(yōu)化實(shí)戰(zhàn)指南(從慢查詢到高性能)
本文詳細(xì)講解了MySQL索引的核心原理、創(chuàng)建原則、失效場景以及優(yōu)化策略,通過SQL示例和執(zhí)行計(jì)劃分析,幫助開發(fā)者設(shè)計(jì)高效索引,提升查詢性能,感興趣的朋友跟隨小編一起看看吧2026-01-01
MySql分頁時(shí)使用limit+order by會(huì)出現(xiàn)數(shù)據(jù)重復(fù)問題解決
在MySQL中我們通常會(huì)采用limit來進(jìn)行翻頁查詢,當(dāng)limit遇到 order by的時(shí)候會(huì)出現(xiàn)數(shù)據(jù)重復(fù)問題,本文就來記錄一下,感興趣的可以了解一下2021-08-08

