Postgres where子句比较时间戳

53 postgresql

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

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

我该怎么做?

a_h*_*ame 97

假设你的意思是timestamp因为datetimePostgres中没有

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

select *
from the_table
where the_timestamp_column::date = date '2015-07-15';
Run Code Online (Sandbox Code Playgroud)

这将从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';
Run Code Online (Sandbox Code Playgroud)

  • 您可以直接将时间戳与日期字符串进行比较。Postgres 足够聪明,可以为您进行转换。`其中 the_timestamp_column &gt;= '2015-07-15' 且 the_timestamp_column &lt; '2015-07-16'` (3认同)
  • 希望我在向大表提交第一个样式查询之前阅读了第二部分...... (2认同)