Mysql-常用日期查詢
阿新 • • 發佈:2019-03-22
今天:
SELECT * FROM A WHERE create_time = create_time(now());
昨天:
SELECT * FROM A WHERE TO_DAYS( NOW( ) ) - TO_DAYS( create_time) <= 1;
最近7天:
SELECT * FROM A where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(create_time);
本週資料:
SELECT * FROM A WHERE YEARWEEK(date_format(create_time,'%Y-%m-%d')) = YEARWEEK(now());
上週資料:
SELECT * FROM A WHERE YEARWEEK(date_format(create_time,'%Y-%m-%d')) = YEARWEEK(now())-1;
最近30天:
SELECT * FROM A where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(create_time);
本月:
SELECT * FROM A WHERE DATE_FORMAT( create_time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' );
上一月:
SELECT * FROM A WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format(create_time, '%Y%m' ) ) =1;
本季度資料:
SELECT * FROM A WHERE QUARTER(create_time)=QUARTER(now());
距離當前6個月的資料:
SELECT * FROM A WHERE create_time BETWEEN date_sub(now(),INTERVAL 6 MONTH) AND now();
上季度資料:
SELECT * FROM A WHERE QUARTER(create_time)=QUARTER(DATE_SUB(now(),INTERVAL 1 QUARTER));
本年度資料:
SELECT * FROM A WHERE YEAR(create_time)=YEAR(NOW());
上年度資料:
SELECT * FROM A WHERE YEAR(create_time)=YEAR(date_sub(now(),INTERVAL 1 YEAR));