在Mysql中获取Unix时间戳为今天的记录。

3

我正在将记录存储在MySQL中,其中一个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时间戳值等于今天日期的记录呢?

谢谢。


你能分享一下数据库的基本结构和一些数据吗? - Jenish
2个回答

4

将查询更改为:

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()

现在它正在工作。


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接受两个边界),


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接