Postgres时差

Md *_*yan 5 postgresql timestamp datepart

我试图使用postgresql从表(login_history为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
Run Code Online (Sandbox Code Playgroud)

它工作正常.但是当我尝试使用此代码从表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
Run Code Online (Sandbox Code Playgroud)

它抛出了这个错误

SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "t1"
Run Code Online (Sandbox Code Playgroud)

谢谢

reu*_*pen 6

我会使用减去两个时间戳的结果来获得更简单的表达式:

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

(给出72)

或者你的桌子:

select extract (epoch from (t1.logout_date - t1.login_date))::integer/60
Run Code Online (Sandbox Code Playgroud)

如果你需要施放:

select extract (epoch from (t1.logout_date::timestamp - t1.login_date::timestamp))::integer/60
Run Code Online (Sandbox Code Playgroud)

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