【问题标题】:Get dates between current date and a given month in postgresql [closed]在postgresql中获取当前日期和给定月份之间的日期[关闭]
【发布时间】:2015-02-22 21:15:20
【问题描述】:

我有一个名为Stock 的表,其中包含药品库存记录。它有一个名为expDate 的列。我想选择当前日期(当前月份)和给定月份之间即将到期的股票

例如:从今天/本月开始,选择 expDate 介于 4 months 之间的股票

我知道我必须为此编写一个查询,并且我知道如何获取两个日期之间的记录。但我不知道写查询到get records between current date and 4 months from the current date

我该怎么做?

【问题讨论】:

  • 要做到这一点你需要编写sql查询
  • @Lashane 我知道我必须为此编写一个查询,并且我知道在两个日期之间获取记录。但我不知道编写查询来获取当前日期和当前日期 4 个月之间的记录。这就是我寻求帮助的原因
  • @CR7 你的评论比你的问题更能描述你的问题。考虑修改。

标签: sql postgresql date datetime timestamp


【解决方案1】:

我会使用这样的东西:

dbname=> create table stock ( medicine character varying ( 32 ), expDate timestamp );
CREATE TABLE
dbname=> insert into stock values ( 'first', '2015-01-01'), ('second', '2016-01-01');
INSERT 0 2
dbname=> select * from stock where expDate < now() + '4 months';
 medicine |       expdate
----------+---------------------
 first    | 2015-01-01 00:00:00
(1 row)

now() 给出当前时间戳,+ '4 months' 增加 4 个月的间隔。

【讨论】:

    猜你喜欢
    • 2017-08-16
    • 1970-01-01
    • 2021-07-17
    • 2018-12-14
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多