如果分钟高于15分钟,则在sql中创建一列

时间:2017-04-05 14:27:30

标签: mysql sql database database-design

我希望在以下代码中的分钟(LT Min)超过15分钟时创建一列

select TIMESTAMPDIFF(MINUTE, cs.subscribeddate,cs.unsubscribeddate) as 'LT Min'
from customer cs 

有什么想法吗?我应该使用IF吗?

1 个答案:

答案 0 :(得分:0)

您已经展示了如何在几分钟内获得差异。现在要么用它来限制选定的记录,例如:

select cs.*, TIMESTAMPDIFF(MINUTE, cs.subscribeddate,cs.unsubscribeddate) as "LT Min"
from customer cs 
where TIMESTAMPDIFF(MINUTE, cs.subscribeddate, cs.unsubscribeddate) > 15;

或选择所有行并仅在高于15时显示差异:

select cs.*, 
  case when TIMESTAMPDIFF(MINUTE, cs.subscribeddate, cs.unsubscribeddate) > 15 then 
    TIMESTAMPDIFF(MINUTE, cs.subscribeddate, cs.unsubscribeddate)
  end as "LT Min"
from customer cs;