How to query month by mysql
This article is about how mysql queries the month. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Mysql query month method: 1, through "select date_format (DATE_SUB (curdate (), INTERVAL 0 MONTH),'% m')" query the current month; 2, through "INTERVAL 1 MONTH" query last month.
The operating environment of this paper is Windows7 system, mysql 5.0and Dell G3.
Mysql queries month data.
/ / View this month's data SELECT*FROMcontent_publishWHEREdate_format (publish_time,'% Y% m') = date_format (DATE_SUB (curdate (), INTERVAL 0 MONTH),'% Y% m') / / View last month's data SELECT*FROMcontent_publishWHEREdate_format (publish_time,'% Y% m') = date_format (DATE_SUB (curdate (), INTERVAL 1 MONTH),'% Y% m') / / query last month's data SELECT*FROMcontent_publishWHEREdate_format (publish_time) % Y% m') = date_format (DATE_SUB (curdate (), INTERVAL 2 MONTH),% Y% m') / query current month select date_format (DATE_SUB (curdate (), INTERVAL 0 MONTH),'% m') / query last month select date_format (DATE_SUB (curdate (), INTERVAL 1 MONTH),'% m') / / query last month select date_format (DATE_SUB (curdate (), INTERVAL 0 MONTH) '% m')
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)