【问题标题】:Select between two timestamps with conditional case在有条件的情况下选择两个时间戳
【发布时间】:2015-12-22 01:45:43
【问题描述】:

我有一个表,想要获取两个时间戳之间的现在(当前时间戳)的记录(如果不为空):

id title start      end
1  A     1450718820 1450719000  
2  B     null       null
3  C     1450718820 null
4  D     null       1450718000  

Example 1: if current timestamp - 1450717990 I need records with ID: 2,4
Example 2: if current timestamp - 1450718830 I need records with ID: 1,2,3
Example 3: if current timestamp - 1450719010 I need records with ID: 2,3

谢谢! startend 是 bigint(20) 字段

【问题讨论】:

    标签: php mysql timestamp case


    【解决方案1】:

    你可以使用如下逻辑:

    select t.*
    from t
    where (start is null or unix_timestamp() >= start) and
          (end is null or unix_timestamp() <= end);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-18
      • 2012-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多