Mysql queries data from the current day, this week, this month, and the previous month.
Today
Select * from table name where to_days (time field name) = to_days (now ());
Yesterday
SELECT * FROM table name WHERE TO_DAYS (NOW ()-TO_DAYS (time field name) <= 1
Last 7 days
SELECT * FROM table name where DATE_SUB (CURDATE (), INTERVAL 7 DAY) <= date (time field name)
Last 30 days
SELECT * FROM table name where DATE_SUB (CURDATE (), INTERVAL 30 DAY) <= date (time field name)
This month
SELECT * FROM table name WHERE DATE_FORMAT (time field name, '% Y % m') = DATE_FORMAT (CURDATE (),' % Y % m ')
Last month
SELECT * FROM table name WHERE PERIOD_DIFF (date_format (now (), '% Y % m'), date_format (time field name,' % Y % m') = 1
Query data for the current quarter
select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());
Query data of the last quarter
select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));
Query current year data
select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());
Query data of the previous year
select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));
Query data for the current week
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());
Query last week's data
SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;
Query data of last month
Select name, submittime from enterprise where date_format (submittime, '% Y-% m') = date_format (DATE_SUB (curdate (), INTERVAL 1 MONTH ), '% Y-% m') select * from user where DATE_FORMAT (pudate,' % Y % m') = DATE_FORMAT (CURDATE (), '% Y % m '); select * from user where WEEKOFYEAR (FROM_UNIXTIME (pudate, '% y-% m-% D') = WEEKOFYEAR (now ()) select * from user where MONTH (FROM_UNIXTIME (pudate, '% y-% m-% D') = MONTH (now ()) select * from user where YEAR (FROM_UNIXTIME (pudate, '% y-% m-% D') = YEAR (now () and MONTH (FROM_UNIXTIME (pudate, '% y-% m-% D') = MONTH (now () select * from user where pudate between last day of last MONTH and first day of next MONTH
Query data of the current month
select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')
Query data 6 months from the current time
select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();
PS: Let's take a look at how mysql queries the information of the current day?
It turns out that you are not familiar with SQL query statements. You can use all of them to search for them. Fortunately, the network provides us with a lot of support. Today, I used another statement, so I really couldn't solve it for a while. I went online and looked at it. I felt like there was one. How was it so simple. There are too many things to be accumulated.
Let's record this simple question today! It is an accumulation:
Mysql queries all information on the current day:
select * from test where year(regdate)=year(now()) and month(regdate)=month(now()) and day(regdate)=day(now())
This is complicated and easy to write:
select * from table where date(regdate) = curdate();
The date () function gets the date part, discards the time part, and then compares it with the current date.