我正在将记录存储在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时间戳值等于今天日期的记录呢?
谢谢。
我正在将记录存储在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时间戳值等于今天日期的记录呢?
谢谢。
将查询更改为:
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()
现在它正在工作。
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
接受两个边界),