mybatis去重小结

在开发中遇到需要记录用户的所有浏览商品记录,但是返回前端记录中需要对重复店铺的重复商品进行去重处理,刚开始使用了distinct,发现返回结果只有product_id,shop_id,shop_type这三个字段有值,其他字段均为null;

 Wrapper<TbUserFootprint> wr = new EntityWrapper<>();
        wr.  setSqlSelect("distinct product_id,shop_id,shop_type")
                .eq("user_id", userId)
                .eq("product_type", productType)
                .eq("del_flag", CommonConstant.STATUS_NORMAL)
                .orderBy("create_time", false)
   List<TbUserFootprint> tbUserFootprintList = this.baseMapper.selectList(wr);

后改用groupBy关键字来处理,具体如下:

      wr .eq("user_id", userId)
                .eq("product_type", productType)
                .eq("del_flag", CommonConstant.STATUS_NORMAL)
              .groupBy("product_id,shop_id,shop_type");
        List<TbUserFootprint> tbUserFootprintList = this.baseMapper.selectList(wr);

总结:在去重查询时,distinct关键字只能返回它的目标字段,而无法同时返回其它字段,要想解决这个问题,可以利用group by按照多列进行嵌套分组。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值