在Mysql中获取unix时间戳今天的记录

时间:2017-09-22 13:24:53

标签: mysql unix-timestamp

我将记录存储在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时间戳值=今天日期的记录?

谢谢,

2 个答案:

答案 0 :(得分: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()

它现在正在工作。

答案 1 :(得分:0)

一般情况下,您希望避免在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接受两个边界)

相关问题