查询以获取上个月的所有行 [英] Query to get all rows from previous month

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

问题描述

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

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)

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆