关于用一个表的字段对另一个表模糊查询的问题

一,问题描述

今天做项目的时候遇到了一个需求,就是按照客户的名字模糊查询合同表。然后看了一下合同表,里面并没有客户名这个字段,有的是一个客户id,所有关于客户的东西都是通过它连表查询出来的,作为一个新手,这显然对我有一定的困难。

二,解决

最终我的解决思路就是——子查询,用in做判断条件。直接上代码:

<select id="selectEsContractList" parameterType="EsContract" resultMap="EsContractResult">
        <include refid="selectEsContractVo"/>
        <where>
            <if test="customer != null ">
                and customer_id in (select id from es_customers where name like concat('%', #{customer}, '%'))
            </if>
            <if test="itemId != null  and itemId != ''"> and c.item_id = #{itemId}</if>
            <if test="buildId != null "> and c.build_id = #{buildId}</if>
            <if test="unitId != null "> and c.unit_id = #{unitId}</if>
            <if test="doorId != null "> and c.door_id = #{doorId}</if>
            <if test="payphase != null  and payphase != ''"> and payphase = #{payphase}</if>
            <if test="deptId != null "> and c.dept_id = #{deptId}</if>
            <if test="userId != null "> and c.user_id = #{userId}</if>
            <if test="userId != null "> and c.channel_id = #{channelId}</if>
        </where>
</select>

这就是那一部分的代码,当传来的customer(客户名)不为空时,if里面的sql语句就会生效,

select id from es_customers where name like concat('%', #{customer}, '%')就会查出所有符合条件的客户(只查了id),当合同表中的记录的customer_id字段在(in)这个查询结果中,就会被查出来。

postman测试,数据因为懒所以只有一个客户, 但从他们合同id不一样可以看出来是正确的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值