多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 表信息 login_location 登录地区 北京, 上海 loginTime 时间 datetime 2020-01-01 返回结果会补日期, 比如本周, 只有周一的数据, 则也会返回周二到周日的数据, 数量统计为0, 其他一样会补日期 ## 本周 纯SQL ``` SELECT a.DAYS as loginTime, count(login_location) num FROM ( SELECT DATE_FORMAT(date, '%Y-%m-%d') AS DAYS FROM ( SELECT CURDATE() - INTERVAL (WEEKDAY(CURDATE()) ) DAY AS date UNION ALL SELECT CURDATE() - INTERVAL (WEEKDAY(CURDATE()) - 1) DAY AS date UNION ALL SELECT CURDATE() - INTERVAL (WEEKDAY(CURDATE()) - 2) DAY AS date UNION ALL SELECT CURDATE() - INTERVAL (WEEKDAY(CURDATE()) - 3) DAY AS date UNION ALL SELECT CURDATE() - INTERVAL (WEEKDAY(CURDATE()) - 4) DAY AS date UNION ALL SELECT CURDATE() - INTERVAL (WEEKDAY(CURDATE()) - 5) DAY AS date UNION ALL SELECT CURDATE() - INTERVAL (WEEKDAY(CURDATE()) - 6) DAY AS date ) AS week_dates ) a LEFT JOIN sys_logininfor ON LEFT ( login_time, 10 ) = a.DAYS GROUP BY a.DAYS ORDER BY loginTime; ``` ## 本月 纯SQL ``` SELECT a.DAYS as loginTime, count(login_location) num FROM ( SELECT date_add( DATE_ADD(CURDATE(), INTERVAL - DAY ( CURDATE() ) DAY ), INTERVAL ( cast( help_topic_id AS signed INTEGER ) + 1 ) DAY ) as DAYS FROM mysql.help_topic WHERE help_topic_id < DAY ( last_day( CURDATE() )) ORDER BY help_topic_id ) a LEFT JOIN sys_logininfor ON LEFT ( login_time, 10 ) = a.DAYS GROUP BY a.DAYS ORDER BY loginTime ``` ## 本季 纯SQL ``` SELECT a.DAYS as loginTime, count(login_location) num FROM ( SELECT date_add( DATE_ADD(CURDATE(), INTERVAL - DAYOFYEAR ( CURDATE() ) DAY ), INTERVAL ( cast( help_topic_id AS signed INTEGER ) ) DAY ) as DAYS FROM mysql.help_topic WHERE help_topic_id <= DAYOFYEAR( LAST_DAY(concat(year(CURDATE()), '-', lpad(QUARTER(CURDATE()) *3,2,0), '-01'))) and help_topic_id >= DAYOFYEAR( concat(year(CURDATE()), '-', lpad((QUARTER(CURDATE()) - 1) *3+1,2,'0'), '-01')) ORDER BY help_topic_id ) a LEFT JOIN sys_logininfor ON LEFT ( login_time, 10 ) = a.DAYS GROUP BY a.DAYS ORDER BY loginTime ``` ## 查询指定年份, 按月返回 MySQL查询出有数据的日期, Java补充缺失的日期, 工具类是之前项目里的 ``` SELECT date_format( login_time, '%Y-%m' ) AS loginTime, count( login_location ) AS num FROM sys_logininfor WHERE date_format( login_time, '%Y' ) = date_format( now(), '%Y' ) GROUP BY loginTime MonthUtil util = new MonthUtil(); List<String> monthArr = util.monthArryear(LocalDate.now().getYear() + ""); List<LoginTimeVo> loginYears = baseMapper.getLoginYears(year); monthArr.forEach(month -> { // 没有这个日期 boolean b = loginYears.stream().noneMatch(s -> Objects.equals(month, s.getLoginTime())); if (b) { LoginTimeVo vo = new LoginTimeVo(); vo.setLoginTime(month); vo.setNum(0); loginYears.add(vo); } }); loginYears.sort(Comparator.comparing(LoginTimeVo::getLoginTime)); return loginYears; ``` 工具类 MonthUtil ``` import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; public class MonthUtil { public List<String> monthArr() { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); Calendar c; List<String> monthArr = new ArrayList<>(); for (int i = 11; i >= 0; i--) { c = Calendar.getInstance(); c.add(Calendar.MONTH, -i); Date start = c.getTime(); monthArr.add(format.format(start)); } return monthArr; } public List<String> monthArryear(String year) { List<String> monthArr = new ArrayList<>(); String month = ""; for (int i = 1; i <= 12; i++) { month = i + ""; if (month.length() == 1) { month = '0' + month + ""; } monthArr.add(year + "-" + month); } return monthArr; } } ```