postgresql Postgres where 子句比较时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31433747/
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-09-09 06:01:48  来源:igfitidea点击:

Postgres where clause compare timestamp

postgresql

提问by a_horse_with_no_name

I have a table where column is of datatype timestamp

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

Which contains records multiple records for a day I want to select all rows corresponding to day

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

How do I do it?

我该怎么做?

回答by a_horse_with_no_name

Assuming you actually mean timestampbecause there is no datetimein Postgres

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

Cast the timestamp column to a date, that will remove the time part:

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

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

This will return all rows from July, 15th.

这将返回从 7 月 15 日开始的所有行。

Note that the above will notuse an index on the_timestamp_column. If performance is critical, you need to either create an index on that expression or use a range condition:

请注意,以上不会在 上使用索引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';