SQL查询之指定日期内的数据。
例如:今天、本周、本月、本季度、今年、近半年、近一个月、近7天
mysql有关日期条件的查询语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
SELECT * FROM `table_name` WHERE TO_DAYS(col_name)=TO_DAYS(NEW());
SELECT * FROM `table_name` WHERE YEARWEEK(DATE_FORMAT(col_name,'%Y-%m-%d'))=YEARWEEK(NEW());
SELECT * FROM `table_name` WHERE DATE_FORMAT(col_name,'%Y-%m')=DATE_FORMAT(NEW(),'%Y-%m');
SELECT * FROM `table_name` WHERE QUARTER(col_name)=QUARTER(NEW());
SELECT * FROM `table_name` WHERE YEAR(col_name)=YEAR(NEW());
SELECT * FROM `table_name` WHERE YEAR(col_name)=2020 AND MONTH(col_name)=6; SELECT * FROM `table_name` WHERE DATE_FORMAT(col_name,'%Y-%m')='2020-06';
SELECT * FROM `table_name` WHERE DATE_SUB(NEW(), INTERVAL 1 MONTH) <= DATE(col_name);
SELECT * FROM `table_name` WHERE DATE_SUB(NEW(), INTERVAL 7 DAY) <= DATE(col_name);
SELECT * FROM `table_name` WHERE YEAR(col_name)=YEAR(DATE_SUB(NEW(),INTERVAL 1 YEAR));
SELECT * FROM `table_name` WHERE QUARTER(col_name)=QUARTER(DATE_SUB(NEW(),INTERVAL 1 QUARTER));
SELECT COUNT(*) as nums, DATE_FORMAT( col_name, '%Y-%m' ) as date_time FROM `table_name` WHERE DATE_SUB( NEW(), INTERVAL 6 MONTH ) <= DATE( col_name ) GROUP BY date_time;
|
向博主反馈问题