MySQL获取日期周、月、天,生成序号

 MySQL时间格式化:  %Y-%m-%d %H:%i:%s.%f

SELECT DATE_FORMAT(2020-04-12 15:23:16.463,%Y-%m-%d %H:%i:%s.%f)
  2020-04-12 15:23:16.463000

 

SELECT @cdate := date_add(@cdate, interval - 1 day) as date FROM(SELECT @cdate := date_add(date_sub(CURDATE(),interval 1 DAY), interval + 1 DAY) from resource_publish) t0 LIMIT 7

SELECT @cdate := date_add(@cdate, interval - 1 day) as date_str FROM(SELECT @cdate := date_add(CURDATE(), interval + 1 DAY) from resource_publish) t0 LIMIT 7

SELECT @cdate := date_add(@cdate, interval - 1 day) as date_str FROM(SELECT @cdate := date_add(‘20200402‘, interval + 1 DAY) from resource_publish) t0 LIMIT 7

SELECT @cdate := date_add(@cdate, interval - 1 MONTH) as date_str FROM(SELECT @cdate := date_add(CURDATE(), interval + 1 MONTH) from resource_publish) t0 LIMIT 12

-- 查询最近七个月每月一号
SELECT @cdate := date_add(@cdate, interval - 1 MONTH) as daytime FROM(SELECT @cdate := date_add(date_sub(CURDATE(),interval 1 MONTH), interval + 1 MONTH) from view_wl018_mx) t0 LIMIT 7;
-- 查询最近七个月每月月末
SELECT @lastDay := last_day( date_add(@lastDay,interval 1 month )) lastDays from (SELECT @lastDay := date_add(curdate(),interval -6 month) from view_wl018_mx limit 6) a;
-- 查询最近七月的月份
SELECT date_format(@lastDay := last_day( date_add(@lastDay,interval 1 month ) ) ,‘%Y-%m‘ ) lastDays from (SELECT @lastDay := date_add(curdate(),interval -6 month) from view_wl018_mx limit 6) a;

-- 获取当年所有月份
SELECT date_format(@lastDay := last_day(date_add(@lastDay,interval 1 month)) ,‘%Y-%m‘) lastDays from (SELECT @lastDay := date_add(curdate(),interval -MONTH(curdate()) month) from view_wl018_mx LIMIT 12) a;

-- 获取当年所有月份
SELECT CASE WHEN length(mon) = 1 THEN concat(LEFT (CURRENT_DATE, 5),‘0‘,mon)  ELSE concat(LEFT(CURRENT_DATE, 5), mon) END months
FROM (SELECT @m :=@m + 1 mon FROM view_wl018_mx,(SELECT @m := 0) a) aa LIMIT 12;

 -- 获取当年第一个月份

SELECT date_format(date_sub(date_sub(curdate(),interval day(curdate()) - 1 day),interval MONTH(CURDATE())-1 month),‘%Y-%m‘);

-- 上月月份
select extract(YEAR_MONTH from date_add(NOW(), interval -1 month));
-- 上年月份
select extract(YEAR_MONTH from date_add(NOW(), interval -1 year));

 

SELECT * FROM(
SELECT daystr,WEEKDAY(daystr) AS wd FROM(
SELECT @cdate := date_add(@cdate, interval - 1 day) as daystr
FROM(SELECT @cdate := date_add(date_sub(CURDATE(),interval 1 DAY), interval + 1 DAY)
from tb_name ) t0 LIMIT 14) ds) aa WHERE wd!=‘5‘ AND wd!=‘6‘    -- 查看最近两周内的工作日

 

set @rownum=0; select @rownum:=@rownum+1 as rownum
select (@i:=@i+1) as rownum FROM (select @i:=0) as rn -- 和其他列组合自动生成序号
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 35 DAY); -- 获取六周前的周一
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 0 DAY); -- 获取当前周所在的周一
select date_sub(‘2020-02-28‘,INTERVAL WEEKDAY(‘2020-02-28‘) - 6 DAY); -- 获取当前周所在的周日

SELECT date_add(DATE_ADD(curdate(),interval -day(curdate())+1 day),interval -6 month) -- 获取六个月前第一天
SELECT DATE_ADD(curdate(),interval -day(curdate())+1 day); -- 获取当月第一天

WEEK(date[,mode])函数
此函数返回日期的周数。双参数的形式WEEK()允许你指定星期是否开始于周日或周一,以及是否返回值应在范围从0到53或从1到53。 如果省略了mode参数,系统default_week_format变量的值被使用。

SELECT WEEK(curdate(),1)

WEEKOFYEAR(date)
返回日期用数字表示的范围是从1到53的日历周。WEEKOFYEAR()是一个兼容性函数,它等效于WEEK(date,3)。
SELECT WEEKOFYEAR(‘2020-3-29‘);


YEARWEEK 是获取年份和周数的一个函数,函数形式为 YEARWEEK(date[,mode])
SELECT YEARWEEK(‘2020-3-29‘);

date_format(date,‘%u‘)
%U 周 (01-53) 星期日是一周的第一天
%u 周 (01-53) 星期一是一周的第一天

DAYOFWEEK 转换日期为周几的函数,1 = Sunday, 2 = Monday,..., 7 = Saturday
select DAYOFWEEK(‘2020-04-05‘); -- 周日为 1
SELECT dayofweek(now()); -- 查看当日是周几

select date_sub(curdate(),interval 1 day) -- 获取前一天日期

#当年第一天:
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);

select curdate(); --获取当前日期
select last_day(curdate()); --获取当月最后一天。
select DATE_ADD(curdate(),interval -day(curdate())+1 day); --获取本月第一天

select date_add(curdate(), interval - day(curdate()) + 1 day); --获取本月第一天
select extract(YEAR_MONTH from date_add(NOW(), interval -1 month)); --获取本月第一天

-- 上月第一天
select date_sub(date_sub(date_format(now(),‘%Y-%m-%d‘),interval extract(day from now())-1 day),interval 1 month);
SELECT date_sub(date_sub(curdate(),interval day(curdate()) - 1 day),interval 1 month);
-- 获取上月月份
SELECT date_format(date_sub(date_sub(curdate(),interval day(curdate()) - 1 day),interval 1 month),‘%Y-%m‘);

select date_format(date_sub(date_sub(curdate(),interval day(curdate()) - 1 day),interval month(curdate())-1 month),‘%y-%m‘);
select date_format(date_add(curdate(),interval -1 month),‘%y-%m‘)


select date_add(curdate()-day(curdate())+1,interval 1 month); -- 获取下个月的第一天
select DATEDIFF(date_add(curdate()-day(curdate())+1,interval 1 month ),DATE_ADD(curdate(),interval -day(curdate())+1 day)) from dual; --获取当前月的天数


#当前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);

SELECT CONCAT(#{year},‘-01‘) AS date UNION
SELECT CONCAT(#{year},‘-02‘) AS date UNION
SELECT CONCAT(#{year},‘-03‘) AS date UNION
SELECT CONCAT(#{year},‘-04‘) AS date UNION
SELECT CONCAT(#{year},‘-05‘) AS date UNION
SELECT CONCAT(#{year},‘-06‘) AS date UNION
SELECT CONCAT(#{year},‘-07‘) AS date UNION
SELECT CONCAT(#{year},‘-08‘) AS date UNION
SELECT CONCAT(#{year},‘-09‘) AS date UNION
SELECT CONCAT(#{year},‘-10‘) AS date UNION
SELECT CONCAT(#{year},‘-11‘) AS date UNION
SELECT CONCAT(#{year},‘-12‘) AS date

-- mysql 查询百分占比,1.字段里嵌套子查询统计总数,2.嵌套临时表统计总数,3.设置临时变量统计总数
SET @sum=(SELECT sum(count) FROM resource_repeatrate);
SELECT `count`,ROUND((count(`count`)/@sum*100),2) AS ct FROM resource_repeatrate GROUP BY `count` ORDER BY ct DESC;

MySQL判空函数:

IFNULL(expr,expr)       处理如果字段为空,返回默认值,sum是统计求和,
IFNULL(SUM(expr1),expr2)  是统计求和之后为空返回默认值
SUM(IFNULL(expr1,expr2))  是判断字段为空返回默认值再求和

作者:Jason Zeng 于 2020-05-14
博客:http://www.cnblogs.com/zengming/ https://blog.csdn.net/Z645817
GItHub:https://github.com/lovelifeming
严正声明:
1.由于本博客部分资源来自互联网,版权均归原作者所有。转载的目的是用于学术交流与讨论学习,将不对任何资源负法律责任。
2.若无意中侵犯到您的版权利益,请来信联系我,我会在收到信息后会尽快给予处理!
3.所有资源内容仅供学习交流之用,请勿用作商业用途,谢谢。
4.如有转发请注明出处,来源于http://www.cnblogs.com/zengming/ https://blog.csdn.net/Z645817,谢谢合作。

MySQL获取日期周、月、天,生成序号

上一篇:MySQL组合索引和最左匹配原则


下一篇:【MongoDB】window10配置