gpt4 book ai didi

time - 将 秒添加到 PostgreSQL 中的

转载 作者:行者123 更新时间:2023-12-02 01:45:49 25 4
gpt4 key购买 nike

我有一个包含以下列的项目表:

  • start_time 列(不带时区的时间戳)
  • expiration_time_seconds 列(整数)

例如,一些值是:

SELECT start_time, expiration_time_seconds 
FROM whatever
ORDER BY start_time;

start_time | expiration_time_seconds
----------------------------+-------------------------
2014-08-05 08:23:32.428452 | 172800
2014-08-10 09:49:51.082456 | 3600
2014-08-13 13:03:56.980073 | 3600
2014-08-21 06:31:38.596451 | 3600
...

如何将过期时间(以秒为单位)添加到 start_time 中?

我尝试为 interval 命令格式化时间间隔字符串,但失败了:

blah=> SELECT interval concat(to_char(3600, '9999'), ' seconds');
ERROR: syntax error at or near "("
LINE 1: SELECT interval concat(to_char(3600, '9999'), ' seconds');

最佳答案

trick是创建一个固定的时间间隔,并将其与列中的秒数相乘:

SELECT start_time, 
expiration_time_seconds,
start_time + expiration_time_seconds * interval '1 second'
FROM whatever
ORDER BY start_time;

start_time | expiration_time_seconds | end_time
----------------------------|-------------------------|----------------------------
2014-08-05 08:23:32.428452 | 172800 | 2014-08-07 08:23:32.428452
2014-08-10 09:49:51.082456 | 3600 | 2014-08-10 10:49:51.082456
2014-08-13 13:03:56.980073 | 3600 | 2014-08-13 14:03:56.980073
2014-08-21 06:31:38.596451 | 3600 | 2014-08-21 07:31:38.596451

关于time - 将 <integer> 秒添加到 PostgreSQL 中的 <timestamp>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28528028/

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