Postgres 时差

Postgres Time Difference

我正在尝试使用 postgresql 从 table(login_history as t1) 检索以分钟为单位的时差。

当我尝试这段代码时

((date_part('hour', timestamp '2014-04-25 09:44:21')- date_part('hour', timestamp '2014-04-25 08:32:21'))*60 +(date_part('minutes', timestamp '2014-04-25 09:44:21')- date_part('minutes', timestamp '2014-04-25 08:32:21'))) as TimeNew

它工作正常。 但是当我尝试使用此代码

从 table t1 检索信息时
((date_part('hour', timestamp t1.login_date)- date_part('hour', timestamp t1.logout_date))*60 +
(date_part('minutes', timestamp t1.login_date)- date_part('minutes', timestamp t1.logout_date))
) as TimeNew

它抛出这个错误

SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "t1"

谢谢

我需要在 t1 之前从查询中删除 timestamp 并且查询有效。

我会使用减去两个时间戳得到的间隔来得到一个更简单的表达式:

select extract (epoch from (timestamp '2014-04-25 09:44:21' - timestamp '2014-04-25 08:32:21'))::integer/60

(给出 72)

或您的table:

select extract (epoch from (t1.logout_date - t1.login_date))::integer/60

如果需要投:

select extract (epoch from (t1.logout_date::timestamp - t1.login_date::timestamp))::integer/60

或查看自定义字符串解析的to_timestamp函数:http://www.postgresql.org/docs/9.4/static/functions-formatting.html