欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MYSQL 时间查询

程序员文章站 2022-09-05 14:51:27
SELECT CURDATE() AS 当前日期;SELECT NOW() AS 当前时间;SELECT QUARTER(CURDATE()) AS 当前季度;SELECT DATE_FORMAT(CURDATE(),'%w') AS 当前周几; --SELECT DATE_FORMAT( SUBDATE(CURDATE(),DATE_FORMAT(CURDATE(),'%w')-1), '%Y-%m-%d 00:00:00') AS 本周一;SELECT DATE_FORMAT( SUBDA.....

SELECT CURDATE() AS 当前日期;
SELECT NOW() AS 当前时间;
SELECT QUARTER(CURDATE()) AS 当前季度;
SELECT DATE_FORMAT(CURDATE(),'%w') AS 当前周几;  -- 
SELECT DATE_FORMAT( SUBDATE(CURDATE(),DATE_FORMAT(CURDATE(),'%w')-1), '%Y-%m-%d 00:00:00') AS 本周一;
SELECT DATE_FORMAT( SUBDATE(CURDATE(),DATE_FORMAT(CURDATE(),'%w')-7), '%Y-%m-%d 23:59:59') AS 本周末;
SELECT DATE_FORMAT(CURDATE(),'%Y-%m-01 00:00:00') AS 本月第一天; 
select concat(date_format(LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-3 month),'%Y-%m-'),'01') AS 本季度第一天;
select LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-1 month) AS 本季度最后一天;

SELECT * FROM report WHERE create_time BETWEEN DATE_FORMAT(CURDATE(),'%Y-%m-01 00:00:00') AND NOW(); -- 本月数据
SELECT * FROM report WHERE DATE_FORMAT(create_time, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m'); -- 本月数据
select * from report where QUARTER(create_time)=QUARTER(now()); -- 本季度数据

本文地址:https://blog.csdn.net/kk_gods/article/details/109000548

相关标签: MYSQL