gpt4 book ai didi

database - 如何在 PostgreSQL 中以毫秒为单位存储时间戳?

转载 作者:太空狗 更新时间:2023-10-30 01:53:44 26 4
gpt4 key购买 nike

我需要在 PostgreSQL 数据库中以 yyyy-mm-dd hh:mm:ss.SSS(SSS 代表毫秒)格式存储时间戳。当然它可以用作文本/或 varchar,但我需要使用时间戳“工作”(例如,过滤某个时间戳之前的所有实例 - 考虑毫秒)。我将如何在 PostgreSQL 中执行此操作?

最佳答案

我知道这是一个老问题,但我遇到了类似的问题。

您可以使用 timestamp(n) 字段,其中 n 等于存储日期的精度位数(timestamp(3) 将为您提供毫秒精度)

示例

CREATE TABLE date_test (datetime timestamp(3) with time zone);

insert into date_test values(to_timestamp(1525745241.879));

select EXTRACT(epoch FROM datetime) from date_test;

注意:在当前版本的 postgres 中,时间戳存储为整数,因此不会引起浮点舍入问题

关于database - 如何在 PostgreSQL 中以毫秒为单位存储时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37139033/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com