Fuzzy Search, MySql MAX, GROUP BY and ORDER BY funtion

When you need a fuzzy seach funciton which only shares one search bar:

<select id="selectAllProducts" resultMap="ProductResult">
    select id, product_no, name, type, unit, brand, remark
    from product_info
    where 1=1
    <if test="(appSearch != null) and (appSearch neq '')">
        and (
        product_no like concat('%\\',#{appSearch},'%')
        or name like concat('%\\',#{appSearch},'%')
        or type like concat('%\\',#{appSearch},'%')
        )
    </if>
</select>

As far as the project goes, there are more requriements needed: join query with other tables and filter the latest data by create_time, return createTime to front end. Group by product number.

    <select id="selectAllProducts" resultMap="ProductResult">
        select * from
        (select a.id, a.product_no, a.name, a.type, a.unit, a.brand, a.remark, pi.product_sign as productSign,pl.name as locationName, max(pi.create_time) as createTime
        from product_info a
        left join product_inbound pi on a.product_no = pi.product_no
        left join product_location pl on pi.product_loc = pl.location
        where 1=1
        <if test="(appSearch != null) and (appSearch neq '')">
            and (
            a.product_no like concat('%\\',#{appSearch},'%')
            or a.name like concat('%\\',#{appSearch},'%')
            or a.type like concat('%\\',#{appSearch},'%')
            )
        </if>
        <if test="(productSign != null) and (productSign neq '')">
            and pi.product_sign = #{productSign}
        </if>
        GROUP BY pi.product_no
        )
        as temp
        ORDER BY temp.createTime desc

    </select>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值