MySQL sql statement on the date the query data

 
Query data between certain dates:

select * from the data table where time field name BETWEEN '2016-02-01' AND '2016-02-05'

Queries forward three months of data:

select * from the data field name table where time BETWEEN DATE_SUB (NOW (), INTERVAL 3 MONTH) AND NOW ()

Previous year's data query:

select * from the data field name table where time BETWEEN DATE_SUB (NOW (), INTERVAL 1 YEAR) AND NOW ()

This month's data query:

select * from the data table where DATE_FORMAT (Time field name, '% Y-% m') = DATE_FORMAT (NOW (), '% Y-% m')

Last month, the data query:

select * from the data table where DATE_FORMAT ( Time field name, '% Y-% m') = DATE_FORMAT (DATE_SUB (CURDATE (), INTERVAL 1 MONTH), '% Y-% m')

This week's data query:

select * from the data table where YEARWEEK (DATE_FORMAT (Time field name, '% Y-% m-% d')) = YEARWEEK (NOW ())

Last week, data query:

select * from 数据表 where YEARWEEK(DATE_FORMAT(CREATE_TIME,'%Y-%m-%d')) = YEARWEEK(NOW())-1

Queries forward seven days of data:

select * from the data table where DATE_SUB (CURDATE (), INTERVAL 7 DAY) <= determination time you field name

Queries forward 30 days of data:

select * from the data table where DATE_SUB (CURDATE (), INTERVAL 30 DAY) <= determination time you field name

According to people accustomed to Monday for the first day of week data used to calculate the week

SELECT * FROM t_browse_statistics WHERE YEARWEEK(date_format(brows_date,'%Y-%m-%d'),1) = YEARWEEK(now())+1
ORDER BY brows_date DESC

select * from the data table where time field name BETWEEN '2016-02-01' AND '2016-02-05'

Queries forward three months of data:

select * from the data field name table where time BETWEEN DATE_SUB (NOW (), INTERVAL 3 MONTH) AND NOW ()

Previous year's data query:

select * from the data field name table where time BETWEEN DATE_SUB (NOW (), INTERVAL 1 YEAR) AND NOW ()

This month's data query:

select * from the data table where DATE_FORMAT (Time field name, '% Y-% m') = DATE_FORMAT (NOW (), '% Y-% m')

Last month, the data query:

select * from the data table where DATE_FORMAT ( Time field name, '% Y-% m') = DATE_FORMAT (DATE_SUB (CURDATE (), INTERVAL 1 MONTH), '% Y-% m')

This week's data query:

select * from the data table where YEARWEEK (DATE_FORMAT (Time field name, '% Y-% m-% d')) = YEARWEEK (NOW ())

Last week, data query:

select * from 数据表 where YEARWEEK(DATE_FORMAT(CREATE_TIME,'%Y-%m-%d')) = YEARWEEK(NOW())-1

Queries forward seven days of data:

select * from the data table where DATE_SUB (CURDATE (), INTERVAL 7 DAY) <= determination time you field name

Queries forward 30 days of data:

select * from the data table where DATE_SUB (CURDATE (), INTERVAL 30 DAY) <= determination time you field name

According to people accustomed to Monday for the first day of week data used to calculate the week

SELECT * FROM t_browse_statistics WHERE YEARWEEK(date_format(brows_date,'%Y-%m-%d'),1) = YEARWEEK(now())+1
ORDER BY brows_date DESC

Guess you like

Origin www.cnblogs.com/trip-j/p/12072673.html