查询以获取上个月的所有行

2021-11-20 00:00:00 mysql

我需要选择数据库中上个月创建的所有行.

I need to select all rows in my database that were created last month.

例如,如果当前月份是一月,那么我想返回在十二月创建的所有行,如果月份是二月,那么我想返回一月创建的所有行.我的数据库中有一个 date_created 列,其中列出了以这种格式创建的日期:2007-06-05 14:50:17.

For example, if the current month is January, then I want to return all rows that were created in December, if the month is February, then I want to return all rows that were created in January. I have a date_created column in my database that lists the date created in this format: 2007-06-05 14:50:17.

推荐答案

SELECT * FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)

相关文章