在 postgresql 中获取上个月的数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37587910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:18:06  来源:igfitidea点击:

Get data for previous month in postgresql

postgresql

提问by Ilian Vasilev Kulishev

SELECT *
FROM Conference WHERE date_start ---don't know how to proceed---

How to check if date_starthas been in the previous month ? Thanks you.

如何检查是否date_start在上个月?谢谢。

回答by a_horse_with_no_name

Subtract one month from the current month, then "truncate" that to the beginning of that date. As you don't want to include rows from "this" month, you also need to add a condition for that

从当前月份减去一个月,然后将其“截断”到该日期的开头。由于您不想包含“这个”月的行,您还需要为此添加一个条件

SELECT *
FROM Conference 
WHERE date_start >= date_trunc('month', current_date - interval '1' month)
  and date_start < date_trunc('month', current_date)

date_trunc('month', current_date - interval '1' month)will return the 1st day of the previous month and date_trunc('month', current_date)will return the first day of "this" month.

date_trunc('month', current_date - interval '1' month)将返回上个月date_trunc('month', current_date)的第一天,并将返回“本”月的第一天。