如何使用knex.js在postgres上处理带时区的时间戳

时间:2016-05-25 05:30:54

标签: postgresql knex.js

我正在尝试将以下sql查询转换为knex:

select count(*) as finished_on_time from task_history 
where  date = 20160303 
       and store_id = 2 
       and (schedule_start_time at time zone 'Australia/sydney' + interval '1' minute * floor (group_duration) )::time >= (finish_time at time zone 'Australia/sydney')::time
  • 日期字段采用yyyymmdd格式

以下是我一直在尝试的knex:

db.table('task_history')
.count('*')
.where({date: request.params.storeid, store_id: request.params.storeid }) 
??????

你可以猜到,我不确定用于处理sql语法的子句[在时区Australia/sydney]。

我一直试图在互联网上找到任何类似的解决方案,但最终到了这里。

1 个答案:

答案 0 :(得分:3)

http://knexjs.org/#Builder-whereRaw

db.table('task_history')
    .count('*')
    .where({date: request.params.storeid, store_id: request.params.storeid })
    .whereRaw("(schedule_start_time at time zone 'Australia/sydney' + interval '1' minute * floor (group_duration) )::time >= (finish_time at time zone 'Australia/sydney')::time")