您的当前位置:首页正文

MYSQL获取当前年、季、月、周第一天、最后一天的日期时间戳

2024-10-18 来源:威能网
MYSQL获取当前年、季、⽉、周第⼀天、最后⼀天的⽇期时间

  因为做⼀些商场某个会员今年的消费分析,所以对sql中时间的获取进⾏了判断。 例如获取今年(即当前年的第⼀天到昨天0时之间)的消费总额。 如果需要时间戳转换,⽤UNIX_TIMESTAMP()函数。

 ⼀ 、下⾯是⼀些mysql的时间获取语句:

#当年第⼀天:

SELECT DATE_SUB(CURDATE(),INTERVAL dayofyear(now())-1 DAY);

#当年最后⼀天:

SELECT concat(YEAR(now()),'-12-31');

#当前week的第⼀天:

select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 1 DAY);

#当前week的最后⼀天:

select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) - 5 DAY);

#前⼀week的第⼀天:

select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 8 DAY);

#前⼀week的最后⼀天:

select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 2 DAY);

#前两week的第⼀天:

select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 15 DAY);

#前两week的最后⼀天:

select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 9 DAY);

#当前month的第⼀天:

SELECT concat(date_format(LAST_DAY(now()),'%Y-%m-'),'01');

#当前month的最后⼀天: SELECT LAST_DAY(now());

#前⼀month的第⼀天:

SELECT concat(date_format(LAST_DAY(now() - interval 1 month),'%Y-%m-'),'01');

#前⼀month的最后⼀天:

SELECT LAST_DAY(now() - interval 1 month);

#前两month的第⼀天:

SELECT concat(date_format(LAST_DAY(now() - interval 2 month),'%Y-%m-'),'01');

#前两month的最后⼀天:

SELECT LAST_DAY(now() - interval 2 month);

#当前quarter的第⼀天:

select concat(date_format(LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-3 month),'%Y-%m-'),'01');

#当前quarter的最后⼀天:

select LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-1 month);

#前⼀quarter的第⼀天:

select concat(date_format(LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-6 month),'%Y-%m-'),'01');

#前⼀quarter的最后⼀天:

select LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-4 month);

#前两quarter的第⼀天:

select concat(date_format(LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-9 month),'%Y-%m-'),'01');

#前两quarter的最后⼀天:

select LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-7 month);

⼆、举⼏个某会员的消费分析的sql语句:

-- 今年的消费总额

SELECT IFNULL(SUM(a.trade_amount)/100,0)

FROM mob_checkout_counter.checkout_record a -- 消费记录表

WHERE card_code = '某会员的标识'

AND checkout_status = ‘消费状态’AND trade_time >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(),INTERVAL dayofyear(now())-1 DAY)) -- 当前年的第⼀天的时间戳AND trade_time <= UNIX_TIMESTAMP(CAST(SYSDATE()AS DATE) - INTERVAL 1 DAY) -- 昨天的0时的时间戳-- 今年的消费笔数SELECT COUNT(*)

FROM mob_checkout_counter.checkout_record a WHERE card_code = '2396998881100009965'AND checkout_status = 1

AND trade_time >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(),INTERVAL dayofyear(now())-1 DAY))AND trade_time <= UNIX_TIMESTAMP(CAST(SYSDATE()AS DATE) - INTERVAL 1 DAY)

-- 今年的客单价

SELECT IFNULL(SUM(a.trade_amount)/100,0)/COUNT(*) FROM mob_checkout_counter.checkout_record aWHERE card_code = '2396998881100009965'AND checkout_status = 1

AND trade_time >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(),INTERVAL dayofyear(now())-1 DAY))AND trade_time <= UNIX_TIMESTAMP(CAST(SYSDATE()AS DATE) - INTERVAL 1 DAY)

因篇幅问题不能全部显示,请点此查看更多更全内容