【问题标题】:Postgres where clause compare timestampPostgres where 子句比较时间戳
【发布时间】:2015-07-15 11:44:07
【问题描述】:

我有一个表,其中列的数据类型为 timestamp

其中包含一天的多条记录 我想选择对应天的所有行

我该怎么做?

【问题讨论】:

    标签: postgresql


    【解决方案1】:

    假设您实际上是指 timestamp,因为 Postgres 中没有 datetime

    将时间戳列转换为日期,这将删除时间部分:

    select *
    from the_table
    where the_timestamp_column::date = date '2015-07-15';
    

    这将返回 7 月 15 日之后的所有行。

    请注意,以上内容将使用the_timestamp_column 上的索引。如果性能至关重要,您需要在该表达式上创建索引或使用范围条件:

    select *
    from the_table
    where the_timestamp_column >= timestamp '2015-07-15 00:00:00'
      and the_timestamp_column < timestamp '2015-07-16 00:00:00';
    

    【讨论】:

    • 希望我在将第一个样式查询提交到大表之前阅读第二部分...
    • 您可以直接将时间戳与日期字符串进行比较。 Postgres 足够聪明,可以为你做演员。 where the_timestamp_column &gt;= '2015-07-15' and the_timestamp_column &lt; '2015-07-16'
    • select * from the_table where the_timestamp_column::date = date '2015-07-15';为我工作。谢谢。
    • 不需要日期'2015-07-15'的演员表!
    • 你可以使用 DATE(the_timestamp_column) select * from the_table where DATE(the_timestamp_column) = date '2015-07-15';
    猜你喜欢
    • 1970-01-01
    • 2021-02-09
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 2020-04-19
    • 1970-01-01
    相关资源
    最近更新 更多