【问题标题】:Get records in Mysql where unix timestamp is today在Mysql中获取今天unix时间戳的记录
【发布时间】:2017-09-22 13:24:52
【问题描述】:

我将记录存储在 msyql 中,其中 resolve_by 列具有 unix 时间戳。

我正在尝试这个查询:

SELECT id FROM tickets WHERE FROM_UNIXTIME('resolve_by','%Y-%m-%d') = CURDATE()

基本的表结构是:

id|resolve_by|date_created
4, 1506092040, 1506084841

但这会返回 0 条记录。如何获取 unix 时间戳值 = 今天日期的记录?

谢谢,

【问题讨论】:

  • 你能分享一些数据的数据库基本结构吗..??

标签: mysql unix-timestamp


【解决方案1】:

从以下位置更改查询:

SELECT id FROM tickets WHERE FROM_UNIXTIME('resolve_by','%Y-%m-%d') = CURDATE()

收件人:

SELECT id FROM tickets WHERE FROM_UNIXTIME(resolve_by,'%Y-%m-%d') = CURDATE()

现在可以了。

【讨论】:

    【解决方案2】:

    一般来说,您会希望避免在 where 条件的列一侧使用函数,因为这很可能会使您的查询失去从索引中受益的资格。

    考虑类似:

    create table test_table ( id varchar(36) primary key, ts timestamp );
    
    insert into test_table (id,ts) values('yesterday',     current_timestamp - interval 1 day);
    insert into test_table (id,ts) values('midnight',      current_date);
    insert into test_table (id,ts) values('now',           current_timestamp);
    insert into test_table (id,ts) values('next midnight', current_date + interval 1 day);
    insert into test_table (id,ts) values('tomorrow',      current_timestamp + interval 1 day);
    
    create index test_table_i1 on test_table (ts);
    
    select *
      from test_table
     where ts >= current_date
       and ts <  current_date + interval 1 day;
    ;
    

    PS:你也可以用

    select *
      from test_table
     where ts between current_date and current_date + interval 1 day;
    

    如果您对排除下一个午夜不挑剔(between 接受两个边界)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-27
      • 2011-06-14
      • 2012-10-19
      • 2018-07-11
      • 1970-01-01
      • 1970-01-01
      • 2011-06-13
      相关资源
      最近更新 更多