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

时间:2023-04-30
本文介绍了查询以获取上个月的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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)

这篇关于查询以获取上个月的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:SQL 性能 UNION 与 OR 下一篇:将数组传递给 MySQL 存储例程

相关文章