sql - 根据 SQL 中耗时返回 true/false

标签 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 返回一个 bool 列?如果超过 4 小时,我希望它为真,如果少于 4 小时,则为假。

最佳答案

您可以直接在查询中比较它

 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

希望对您有所帮助。

关于sql - 根据 SQL 中耗时返回 true/false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50147987/

相关文章:

python - 如何在 alembic upgrade head 中运行升级后代码

sql - 如何使 PostgreSQL 不区分大小写?

sql - mySQL 中的空值

postgresql - Postgres : 'role does not exist' using dblink, 但确实如此

PHP - CSV 到表未按预期转换

sql - 生成序列号,跳过空值

postgresql - postgres 在触发器中插入之前更新新变量

sql - Postgres - 在从表中选择值时在 INSERT INTO 查询中插入时间戳?

php - 特定条件下的 MySQL COUNT() 总帖子数?

mysql - MySQL中的语法错误,但是在创建ROLE时语法正确