赞
踩
select now()
select DATE(CURDATE())
select date_format(NOW(),'%Y-%m')
select YEAR(CURDATE())
select MONTH(CURDATE())
select DAYOFMONTH(NOW())
select dayofweek(NOW()) -1
select QUARTER(now())
select current_time()
select DATE_FORMAT(now(),'%Y-%m-%d %h:%i:%s')
查询昨天的数据
select * from test as 'time' where time.update_time <DATE_SUB(CURDATE(), INTERVAL 1 DAY)
查询当前这周的数据
select * from test as 'time' where time.update_time<DATE_SUB(CURDATE(), INTERVAL 0 WEEK)
select * FROM test as 'time' where YEARWEEK(date_format(time.update_time,’%Y-%m-%d’)) = YEARWEEK(now())
查询上一周的数据
select * from test as 'time' where time.update_time<DATE_SUB(CURDATE(), INTERVAL 1 WEEK)
select * FROM test as 'time' WHERE YEARWEEK(date_format(time.update_time,’%Y-%m-%d’)) = YEARWEEK(now())-1
查询上一个月的数据
select * from test as 'time' where time.update_time<DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
查询近两个月的数据(INTERVAL后面的数字改一下就可以了)
select * from test as 'time' where time.update_time<DATE_SUB(CURDATE(), INTERVAL 2 MONTH)
查询上一年的数据
select * from test as 'time' where time.update_time<DATE_SUB(CURDATE(), INTERVAL 1 YEAR)
查询距离当前现在6个月的数据
select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();
查询今天的数据
select * from test as time where to_days(time.update_time ) =to_days(now());
查询本周的数据
select * FROM test as time WHERE YEARWEEK(date_format(time.update_time ,'%Y-%m-%d')) = YEARWEEK(now());
查询本月的数据
select * FROM test as time WHERE DATE_FORMAT(time.update_time , '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )
查询本季度的数据
select * from test as time where QUARTER(time.update_time)=QUARTER(now());
查询本年的数据
select * from test as time where YEAR(time.update_time)=YEAR(NOW());
UNION
连接函数 -- 近1个月
SELECT DATE_FORMAT( CURDATE( ), '%Y-%m' ) AS `month` UNION
-- 近2个月
SELECT DATE_FORMAT( ( CURDATE( ) - INTERVAL 1 MONTH ), '%Y-%m' ) AS `month` UNION
-- 近3个月
SELECT DATE_FORMAT( ( CURDATE( ) - INTERVAL 2 MONTH ), '%Y-%m' ) AS `month` UNION
SELECT DATE_FORMAT( ( CURDATE( ) - INTERVAL 3 MONTH ), '%Y-%m' ) AS `month` UNION
SELECT DATE_FORMAT( ( CURDATE( ) - INTERVAL 4 MONTH ), '%Y-%m' ) AS `month` UNION
-- 近N个月,N等于"INTERVAL 5 MONTH"中间的数值`5`加`1`,此处N=6
SELECT DATE_FORMAT( ( CURDATE( ) - INTERVAL 5 MONTH ), '%Y-%m' ) AS `month`
SELECT CONCAT(RIGHT(v.`month`,2),'月') AS `time`
FROM
(
-- 近1个月
SELECT DATE_FORMAT( CURDATE( ), '%Y-%m' ) AS `month` UNION
-- 近2个月
SELECT DATE_FORMAT( ( CURDATE( ) - INTERVAL 1 MONTH ), '%Y-%m' ) AS `month` UNION
-- 近3个月
SELECT DATE_FORMAT( ( CURDATE( ) - INTERVAL 2 MONTH ), '%Y-%m' ) AS `month` UNION
SELECT DATE_FORMAT( ( CURDATE( ) - INTERVAL 3 MONTH ), '%Y-%m' ) AS `month` UNION
SELECT DATE_FORMAT( ( CURDATE( ) - INTERVAL 4 MONTH ), '%Y-%m' ) AS `month` UNION
-- 近N个月,N等于"INTERVAL 5 MONTH"中间的数值`5`加`1`,此处N=6
SELECT DATE_FORMAT( ( CURDATE( ) - INTERVAL 5 MONTH ), '%Y-%m' ) AS `month`
)
AS v
SELECT DATE_FORMAT(a.create_time, '%Y-%m') AS `month`,COUNT( * ) AS `count`
FROM `user` AS a
-- 计算近六个月的时间
WHERE DATE_FORMAT(a.create_time, '%Y-%m') > DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 6 MONTH ), '%Y-%m')
-- 补充查询匹配条件
AND a.is_deleted = 1
GROUP BY `month`
SELECT DATE_FORMAT(( CURDATE( ) - INTERVAL 0 MONTH ), '%Y-%m') AS `month`,COUNT( * ) AS `count`
FROM `user` AS a
-- 计算12月及其之前的时间 (现在2022年12月份 DATE_FORMAT(( CURDATE( ) - INTERVAL 1 MONTH ), '%Y-%m') = 2022-11)
-- 这是是需要计算 一月份之前,要包含12月份
WHERE DATE_FORMAT(a.create_time, '%Y-%m') < DATE_FORMAT(( CURDATE( ) - INTERVAL -1 MONTH ), '%Y-%m')
-- 补充查询匹配条件
AND a.is_deleted = 1
SELECT DATE_FORMAT(( CURDATE( ) - INTERVAL 0 MONTH ), '%Y-%m') AS `month`,COUNT( * ) AS `count` FROM `user` AS a -- 计算12月及其之前的时间 (现在2022年12月份 DATE_FORMAT(( CURDATE( ) - INTERVAL 1 MONTH ), '%Y-%m') = 2022-11) -- 这是是需要计算 一月份之前,要包含12月份 WHERE DATE_FORMAT(a.create_time, '%Y-%m') < DATE_FORMAT(( CURDATE( ) - INTERVAL -1 MONTH ), '%Y-%m') -- 补充查询匹配条件 AND a.is_deleted = 1 AND a.jurisdiction = 2 UNION -- 下面都是cv了,sql没想到什么好的办法,但在mybatis中可以优化,之后我会讲到 SELECT DATE_FORMAT(( CURDATE( ) - INTERVAL 1 MONTH ), '%Y-%m') AS `month`,COUNT( * ) AS `count` FROM `user` AS a WHERE DATE_FORMAT(a.create_time, '%Y-%m') < DATE_FORMAT(( CURDATE( ) - INTERVAL 0 MONTH ), '%Y-%m') AND a.is_deleted = 1 AND a.jurisdiction = 2 UNION SELECT DATE_FORMAT(( CURDATE( ) - INTERVAL 2 MONTH ), '%Y-%m') AS `month`,COUNT( * ) AS `count` FROM `user` AS a WHERE DATE_FORMAT(a.create_time, '%Y-%m') < DATE_FORMAT(( CURDATE( ) - INTERVAL 1 MONTH ), '%Y-%m') AND a.is_deleted = 1 AND a.jurisdiction = 2 UNION SELECT DATE_FORMAT(( CURDATE( ) - INTERVAL 3 MONTH ), '%Y-%m') AS `month`,COUNT( * ) AS `count` FROM `user` AS a WHERE DATE_FORMAT(a.create_time, '%Y-%m') < DATE_FORMAT(( CURDATE( ) - INTERVAL 2 MONTH ), '%Y-%m') AND a.is_deleted = 1 AND a.jurisdiction = 2 UNION SELECT DATE_FORMAT(( CURDATE( ) - INTERVAL 4 MONTH ), '%Y-%m') AS `month`,COUNT( * ) AS `count` FROM `user` AS a WHERE DATE_FORMAT(a.create_time, '%Y-%m') < DATE_FORMAT(( CURDATE( ) - INTERVAL 3 MONTH ), '%Y-%m') AND a.is_deleted = 1 AND a.jurisdiction = 2 UNION SELECT DATE_FORMAT(( CURDATE( ) - INTERVAL 5 MONTH ), '%Y-%m') AS `month`,COUNT( * ) AS `count` FROM `user` AS a WHERE DATE_FORMAT(a.create_time, '%Y-%m') < DATE_FORMAT(( CURDATE( ) - INTERVAL 4 MONTH ), '%Y-%m') AND a.is_deleted = 1 AND a.jurisdiction = 2
SELECT IFNULL( b.count, 0 ) AS `count`,CONCAT(RIGHT(v.`month`,2),'月') AS `time` FROM ( -- 近1个月 SELECT DATE_FORMAT( CURDATE( ), '%Y-%m' ) AS `month` UNION -- 近2个月 SELECT DATE_FORMAT( ( CURDATE( ) - INTERVAL 1 MONTH ), '%Y-%m' ) AS `month` UNION -- 近3个月 SELECT DATE_FORMAT( ( CURDATE( ) - INTERVAL 2 MONTH ), '%Y-%m' ) AS `month` UNION SELECT DATE_FORMAT( ( CURDATE( ) - INTERVAL 3 MONTH ), '%Y-%m' ) AS `month` UNION SELECT DATE_FORMAT( ( CURDATE( ) - INTERVAL 4 MONTH ), '%Y-%m' ) AS `month` UNION -- 近N个月,N等于"INTERVAL 5 MONTH"中间的数值`5`加`1`,此处N=6 SELECT DATE_FORMAT( ( CURDATE( ) - INTERVAL 5 MONTH ), '%Y-%m' ) AS `month` ) AS v LEFT JOIN ( SELECT DATE_FORMAT(( CURDATE( ) - INTERVAL 0 MONTH ), '%Y-%m') AS `month`,COUNT( * ) AS `count` FROM `user` AS a -- 计算12月及其之前的时间 (现在2022年12月份 DATE_FORMAT(( CURDATE( ) - INTERVAL 1 MONTH ), '%Y-%m') = 2022-11) -- 这是是需要计算 一月份之前,要包含12月份 WHERE DATE_FORMAT(a.create_time, '%Y-%m') < DATE_FORMAT(( CURDATE( ) - INTERVAL -1 MONTH ), '%Y-%m') -- 补充查询匹配条件 AND a.is_deleted = 1 AND a.jurisdiction = 2 UNION -- 下面都是cv了,sql没想到什么好的办法,但在mybatis中可以优化,之后我会讲到 SELECT DATE_FORMAT(( CURDATE( ) - INTERVAL 1 MONTH ), '%Y-%m') AS `month`,COUNT( * ) AS `count` FROM `user` AS a WHERE DATE_FORMAT(a.create_time, '%Y-%m') < DATE_FORMAT(( CURDATE( ) - INTERVAL 0 MONTH ), '%Y-%m') AND a.is_deleted = 1 AND a.jurisdiction = 2 UNION SELECT DATE_FORMAT(( CURDATE( ) - INTERVAL 2 MONTH ), '%Y-%m') AS `month`,COUNT( * ) AS `count` FROM `user` AS a WHERE DATE_FORMAT(a.create_time, '%Y-%m') < DATE_FORMAT(( CURDATE( ) - INTERVAL 1 MONTH ), '%Y-%m') AND a.is_deleted = 1 AND a.jurisdiction = 2 UNION SELECT DATE_FORMAT(( CURDATE( ) - INTERVAL 3 MONTH ), '%Y-%m') AS `month`,COUNT( * ) AS `count` FROM `user` AS a WHERE DATE_FORMAT(a.create_time, '%Y-%m') < DATE_FORMAT(( CURDATE( ) - INTERVAL 2 MONTH ), '%Y-%m') AND a.is_deleted = 1 AND a.jurisdiction = 2 UNION SELECT DATE_FORMAT(( CURDATE( ) - INTERVAL 4 MONTH ), '%Y-%m') AS `month`,COUNT( * ) AS `count` FROM `user` AS a WHERE DATE_FORMAT(a.create_time, '%Y-%m') < DATE_FORMAT(( CURDATE( ) - INTERVAL 3 MONTH ), '%Y-%m') AND a.is_deleted = 1 AND a.jurisdiction = 2 UNION SELECT DATE_FORMAT(( CURDATE( ) - INTERVAL 5 MONTH ), '%Y-%m') AS `month`,COUNT( * ) AS `count` FROM `user` AS a WHERE DATE_FORMAT(a.create_time, '%Y-%m') < DATE_FORMAT(( CURDATE( ) - INTERVAL 4 MONTH ), '%Y-%m') AND a.is_deleted = 1 AND a.jurisdiction = 2 AND a.is_deleted = 1 AND a.jurisdiction = 2 ) AS b ON v.`month` = b.`month` GROUP BY `time` ORDER BY `time` ASC
@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class PeopleCountVO {
private Integer count;
private String time;
private Integer jurisdiction;
private List<Integer> math;
}
<select id="peopleCount" parameterType="com.zhao.pojo.vo.PeopleCountVO" resultType="com.zhao.pojo.vo.PeopleCountVO"> SELECT IFNULL( b.count, 0 ) AS `count`,v.`month` AS `time` FROM ( -- 查询 近六个月 <foreach collection="people.math" separator=" UNION " item="item"> SELECT DATE_FORMAT( ( CURDATE( ) - INTERVAL #{item} - 1 MONTH ), '%Y-%m' ) AS `month` </foreach> ) AS v LEFT JOIN ( <foreach collection="people.math" separator=" UNION " item="item"> SELECT DATE_FORMAT(( CURDATE( ) - INTERVAL #{item}-1 MONTH ), '%Y-%m') AS `month`,COUNT( * ) AS `count` FROM `user` AS a -- 小于符号 在xml中会报错 使用 < 转义一下 WHERE DATE_FORMAT(a.create_time, '%Y-%m') < DATE_FORMAT(( CURDATE( ) - INTERVAL #{item}-2 MONTH ), '%Y-%m') -- 补充查询匹配条件 AND a.is_deleted = 1 AND a.jurisdiction = #{people.jurisdiction} </foreach> ) AS b ON v.`month` = b.`month` GROUP BY `time` ORDER BY `time` ASC </select>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。