zbb20190613 mysql query the same day this week, this month, last month's data

 

Nowadays

select * from table where to_days (time field name) = to_days (now ());

yesterday

SELECT * FROM table name WHERE TO_DAYS (NOW ()) - TO_DAYS (Time field) <= 1

The past 7 days

SELECT * FROM table where DATE_SUB (CURDATE (), INTERVAL 7 DAY) <= date (time field)

Nearly 30 days

SELECT * FROM table where DATE_SUB (CURDATE (), INTERVAL 30 DAY) <= date (time field)

this month

SELECT * FROM table name WHERE DATE_FORMAT (Time field name, '% Y% m') = DATE_FORMAT (CURDATE (), '% Y% m')

Previous Month

SELECT * FROM table name WHERE PERIOD_DIFF (date_format (now (), '% Y% m'), date_format (Time field name, '% Y% m')) = 1

This quarterly data query

select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());

Queries on quarterly data

select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));

Data Query year

select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());

Queries prior year data

select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));

Query the current data this week

SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());

Last week's data query

SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;

Last month's data query

Copy the code
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 上月最后一天 and 下月第一天
Copy the code

Query data for the current month 

select name,submittime from enterprise   where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')

Now the query from the current six-month data

select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();

 

Nowadays

select * from table where to_days (time field name) = to_days (now ());

yesterday

SELECT * FROM table name WHERE TO_DAYS (NOW ()) - TO_DAYS (Time field) <= 1

The past 7 days

SELECT * FROM table where DATE_SUB (CURDATE (), INTERVAL 7 DAY) <= date (time field)

Nearly 30 days

SELECT * FROM table where DATE_SUB (CURDATE (), INTERVAL 30 DAY) <= date (time field)

this month

SELECT * FROM table name WHERE DATE_FORMAT (Time field name, '% Y% m') = DATE_FORMAT (CURDATE (), '% Y% m')

Previous Month

SELECT * FROM table name WHERE PERIOD_DIFF (date_format (now (), '% Y% m'), date_format (Time field name, '% Y% m')) = 1

This quarterly data query

select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());

Queries on quarterly data

select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));

Data Query year

select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());

Queries prior year data

select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));

Query the current data this week

SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());

Last week's data query

SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;

Last month's data query

Copy the code
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 上月最后一天 and 下月第一天
Copy the code

Query data for the current month 

select name,submittime from enterprise   where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')

Now the query from the current six-month data

select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();

 

Guess you like

Origin www.cnblogs.com/super-admin/p/11016293.html