Problem:
MySQL table in a column create_time, type datetime (YYYY-MM-DD HH:MM:SS), want to get data from today to yesterday last month.
SELECT * FROM table name where date_format (create_time, '%y-%m-%d ') between date_sub (Curdate (), Interval 1 MONTH) and Date_sub (Curdate (), Interval 1 day)
After careful study, MySQL has a lot of time types.
/* Today */
SELECT * FROM table name where To_days (Time field) = To_days (now ());
/* Yesterday */
SELECT * FROM table name where To_days (now ())-to_days (Time field) = 1;
/* Nearly 7 days */
SELECT * FROM table name where Date_sub (Curdate (), Interval 7 day) <= Date (Time field);
/* Query data from current 6 months */
SELECT * from table name where Time field between Date_sub (now (), Interval 6 month) and now ();
/* Query the current week's data */
SELECT * FROM table name where Yearweek (Date_format (Time field, '%y-%m-%d ')) = Yearweek (now ());
/* Query last week's data */
SELECT * FROM table name where Yearweek (Date_format (Time field, '%y-%m-%d ')) = Yearweek (now ())-1;
/* Query data for the current month */
SELECT * FROM table name where Date_format (Time field, '%y-%m ') =date_format (now (), '%y-%m ');
/* Query last month's data */
SELECT * FROM table name where Date_format (Time field, '%y-%m ') =date_format (Date_sub (Curdate (), Interval 1 month), '%y-%m ');
Mysql asks for time between yesterday and last month's Today's time function