当前位置: 代码网 > it编程>数据库>Mysql > Mysql的instr()函数用法及说明

Mysql的instr()函数用法及说明

2026年01月04日 Mysql 我要评论
mysql的instr()函数用法mysql的内置函数instr(filed,str)作用是返回str子字符串在filed字符串的第一次出现的位置。当instr(filed,str)=0时表示子符串s

mysql的instr()函数用法

mysql的内置函数instr(filed,str)

作用是返回str子字符串在filed字符串的第一次出现的位置。

当instr(filed,str)=0时

表示子符串str不存在于字符串filed中,因此可以用来实现mysql中的模糊查询,与like用法类似。

如下:

 // 1、instr()函数,#{name}为参数
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>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com