mysql查询指定日期每一天的数据

@RequestMapping(value = "/queryWorkScore", method = RequestMethod.GET)
    public List queryWorkScore(String startTime, String endTime) {
          
   
        int num = calcBetweenDate(startTime, endTime);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("select date_add(?,interval @i:=@i+1 day) as date 
" +
                " from (
 " +
                " select 1 ");
        for (int i = 0; i < num; i++) {
          
   
            stringBuilder.append(" union all select 1 
");
        }
        stringBuilder.append(" ) as tmp, (select @i:= -1) t");
        List<Map<String, Object>> queryForList = jdbcTemplate.queryForList(stringBuilder.toString(), new Object[]{
          
   startTime});

        return queryForList;
    }

    public int calcBetweenDate(String start, String end) {
          
   
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = null;
        Date endDate = null;
        try {
          
   
            startDate = df.parse(start);
            endDate = df.parse(end);
        } catch (Exception e) {
          
   
            System.out.println("日期转换出错");
        }
        int count = (int) ((endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000));
        return count;
    }
经验分享 程序员 微信小程序 职场和发展