mybatis-plus和数据库如何对varchar类型的数据进行数字模式排序

当我查询收缩压和舒张压的最大最小值的时候,我发现查询出的最大最小值并不是实际的数值,于是我去看了下表设计,原来字段value的类型是varchar型的,所以sql应该如下ORDER BY value+0 asc

SELECT  
id,customer_id,metric,value
FROM 
customer_body_metrics_2
WHERE 
(
metric = "diastolicPressure" 
AND customer_id = "1559017565590265856" 
)
ORDER BY value+0 asc

换成mybatis-plus就修改成下面的版本了

@Override
    @UseDataSource(type = DataSourceTypeEnum.SHARDING_DATASOURCE)
    public Double getMax(String customerId, LocalDateTime firstDay, LocalDateTime lastDay, String metric) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("getMax");
        LambdaQueryWrapper<CustomerBodyMetricsEntity> wrapper = this.getWrapper(metric);
        wrapper.eq(CustomerBodyMetricsEntity::getCustomerId, customerId)
                .ge(CustomerBodyMetricsEntity::getVersion, DateTimeUtil.dateTimeToTimestamp(firstDay))
                .le(CustomerBodyMetricsEntity::getVersion, DateTimeUtil.dateTimeToTimestamp(lastDay));
        wrapper.apply("ORDER BY value+0 desc");
        wrapper.last(DaoConst.SQL_LIMIT_ONE);
        CustomerBodyMetricsEntity one = this.getOne(wrapper);
        stopWatch.stop();
        log.debug("content getMax time{}s",stopWatch.getTotalTimeSeconds());
        if (one != null) {
            return Double.valueOf(one.getValue());
        }
        return 0D;
    }

但是报错了,逻辑sql是这样的:

SELECT  id,customer_id,metric,value,unit,label,proportion,reference,label_ranges,env,version,tag,datasource_id,comment,status,tenant_id,create_time,update_time  
FROM customer_body_metrics 
 WHERE (metric = ? AND customer_id = ? AND version >= ? AND version <= ? AND ORDER BY value+0 desc) limit 1

把sql复制到数据库去运行,果然不一样,查看了下,原来是最后多了个“and”
于是我去查看了下代码,原来是last不会加“and”,apply会加“and”,于是有进行了修改
修改之后的代码

  @Override
    @UseDataSource(type = DataSourceTypeEnum.SHARDING_DATASOURCE)
    public Double getMax(String customerId, LocalDateTime firstDay, LocalDateTime lastDay, String metric) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("getMax");
        LambdaQueryWrapper<CustomerBodyMetricsEntity> wrapper = this.getWrapper(metric);
        wrapper.eq(CustomerBodyMetricsEntity::getCustomerId, customerId)
                .ge(CustomerBodyMetricsEntity::getVersion, DateTimeUtil.dateTimeToTimestamp(firstDay))
                .le(CustomerBodyMetricsEntity::getVersion, DateTimeUtil.dateTimeToTimestamp(lastDay));
        wrapper.last("ORDER BY value+0 desc"+DaoConst.SQL_LIMIT_ONE);
        CustomerBodyMetricsEntity one = this.getOne(wrapper);
        stopWatch.stop();
        log.debug("content getMax time{}s",stopWatch.getTotalTimeSeconds());
        if (one != null) {
            return Double.valueOf(one.getValue());
        }
        return 0D;
    }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值