springboot+mysql日期分组查询

springboot+mysql 日期分组查询

1.按照天分组查询

@Query(value = "select DATE_FORMAT(exam_time,'%Y-%m-%d') as time,count(*) as count from exam  where exam_time >=:startTime and exam_time<=:endTime group by time", nativeQuery = true)
List<Map<String, Object>> countTrainingByDay(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);

其中exam_time是表exam中的字段,startTime、endTime是变量。
查询结果如下:
在这里插入图片描述

将startTime与endTime之间时间的数据按照天为单位分组查询,其中只输出数值大于0的map。在需要使用对应天数的数值时,需要先把Object转化为string,在将string转化为int使用。
提取count

parseInt(count.get(i).get("count").toString()) 

2.按照月分组查询

@Query(value = "select DATE_FORMAT(exam_time,'%Y-%m') as time,count(*) as count from exam where exam_time >=:startTime and exam_time<=:endTime group by time", nativeQuery = true)
List<Map<LocalDateTime, Object>> countTrainingByMonth(@Param("startTime"
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值