根据SQL的已用时间返回true / false

时间:2018-05-03 06:04:33

标签: sql postgresql

假设我们有一个SQL查询,如下面的

select now()-insert_time AS time_elapsed, *
from account
where enabled = true AND machine = 'one'
limit 1

是否可以返回now()-insert_time AS time_elapsed的布尔列?如果它超过4小时我希望它是真的,如果它不到4小时我就是假的。

1 个答案:

答案 0 :(得分:3)

您可以直接在查询中进行比较

 select now()-insert_time AS time_elapsed,
        now()-insert_time >= '4 hours' as status,
        * 
   from account 
  where enabled = true 

通常,您将其放在CASE表达式中以获取其他值输出

 select now()-insert_time AS time_elapsed,
        CASE 
             WHEN now()-insert_time >= '4 hours' THEN 'Greater than 4 hours'
             ELSE 'Less than 4 hours'
        END as status,
        * 
   from account 
  where enabled = true 
        AND machine = 'one'  
  limit 1

希望它会对你有帮助。