一、根据当前时间获取到三天前截止到此分秒的数据(以当前时分秒进行过滤)
SELECT
submit_date
FROM
crm_work_order
WHERE
submit_date BETWEEN ( SELECT now( ) - INTERVAL '3 day' )
AND ( SELECT now( ) )
ORDER BY submit_date ASC
二、根据当前时间获取到三天前凌晨的数据(不以当前的时分秒进行过滤)
SELECT
submit_date
FROM
crm_work_order
WHERE
submit_date >= to_timestamp( SUBSTRING ( to_char( now( ), 'yyyy-MM-dd hh24:MI:ss' ) FROM 1 FOR 10 ), 'yyyy-MM-dd' ) - INTERVAL '3 day'
ORDER BY submit_date ASC
三、获得两个日期的时间差
interval_value:相差的小时差
day_value:相差的天数
minutes:相差的分钟数
SELECT
interval_value,
date_part( 'day', interval_value ) AS day_value,
date_part( 'day', interval_value ) * 24 * 60 + date_part( 'minute', interval_value ) AS minutes
FROM
( SELECT ( CURRENT_TIMESTAMP - to_timestamp( '2021-08-10 11:30', 'yyyy-mm-dd hh24:mi' ) ) AS interval_value ) s;
业务上使用
查询出系统当前时间和数据库中服务完成时间差超过10分钟的
SELECT
crm_work_order_relation.*
FROM
crm_work_order_relation
LEFT JOIN crm_work_order ON crm_work_order_relation.crm_work_order = crm_work_order.ID
WHERE
crm_work_order_relation.employee = ? 1
AND crm_work_order_relation.is_remote IS FALSE
AND crm_work_order_relation.status = 5
AND crm_work_order.crm_serve_repair_id IS NOT NULL
AND crm_work_order_relation.complete_time > '2021-07-20'
AND (
date_part( 'day', CURRENT_TIMESTAMP - to_timestamp( crm_work_order_relation.complete_time :: TEXT, 'yyyy-MM-dd hh24:MI:ss' ) ) * 24 * 60 + date_part( 'minute', CURRENT_TIMESTAMP - to_timestamp( crm_work_order_relation.complete_time :: TEXT, 'yyyy-MM-dd hh24:MI:ss' ) ) > 10
)