在 PostgreSQL 中以秒为单位查找时间戳之间的差异

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

Find difference between timestamps in seconds in PostgreSQL

postgresql

提问by Arun

I have a table in PostgreSQL 8.3with 2 timestampcolumns. I would like to get the difference between these timestampsin seconds. Could you please help me how to get this done?

我有一个PostgreSQL 8.3包含 2timestamp列的表格。我想timestamps在几秒钟内得到这些之间的区别。你能帮我怎么做吗?

TableA
(
  timestamp_A timestamp,
  timestamp_B timestamp
)

I need to get something like (timestamo_B - timestamp_A)in seconds (not just the difference between seconds, it should include hours, minutes etc).

我需要(timestamo_B - timestamp_A)在几秒钟内得到一些东西(不仅仅是秒之间的差异,它应该包括小时、分钟等)

回答by Igor Romanchenko

Try: 

尝试: 

SELECT EXTRACT(EPOCH FROM (timestamp_B - timestamp_A))
FROM TableA

Details here: EXTRACT.

详情在这里:提取

回答by Clodoaldo Neto

select age(timestamp_A, timestamp_B)


Answering to Igor's comment:

回答伊戈尔的评论:

select age('2013-02-28 11:01:28'::timestamp, '2011-12-31 11:00'::timestamp);
              age              
-------------------------------
 1 year 1 mon 28 days 00:01:28

回答by tausif

SELECT (cast(timestamp_1 as bigint) - cast(timestamp_2 as bigint)) FROM table;

In case if someone is having an issue using extract.

如果有人在使用extract 时遇到问题。