【问题标题】:Clickhouse datetime comparison not working as expectedClickhouse 日期时间比较未按预期工作
【发布时间】:2020-03-04 05:18:23
【问题描述】:

我正在尝试在 clickhouse 中比较日期时间。但它似乎以某种有线方式工作。 我的表中有一个列,我想与 (now(),'UTC') 进行比较。 如果该列中 datetime 的值小于 (now(),'UTC') 时间,那么我想从该记录中选择数据。

我已经创建了这样的表

create table my_table (`mytime` DateTime, `data' [type]) ENGINE= engine

我想要这样的队列 Select data from my_table where mytime < toDateTime(now(), 'UTC')

即使 mytime > toDateTime(now(), 'UTC') 它总是考虑 mytime

【问题讨论】:

  • toDateTime(now(), 'UTC') - 没有意义。 now() -- 已经是 UTC。存储在表中的 DateTime 是 UTC 中的 unixtime。 DateTime('UTC') -- 表示转换为String时转换为UTC。

标签: database datetime clickhouse


【解决方案1】:

这个问题背后的原因是在 clickhouse 中,它将 DateTime 和 DateTime('UTC') 作为不同的对象,因此它们之间的比较不能按预期工作。由于我想与 (now(),'UTC') 进行比较,因此我必须更改 mytimeDateTime ('UTC')

我必须将表格更改为

create table my_table (`mytime` DateTime ('UTC'), `data' [type]) ENGINE= engine

【讨论】:

    【解决方案2】:

    在我看来,您插入数据的方式可能有问题,或者您的 ClickHouse 版本存在错误。

    以下示例显示了如何以在我的 19.15.4.10 服务器上按预期仅选择较早行的方式执行您正在尝试的操作。请注意 select sleep() 以确保 now() 调用不同。

    drop table if exists my_table;
    create table my_table (mytime DateTime, data String) engine = Memory;
    insert into my_table values(now(), 'a');
    select sleep(1);
    insert into my_table values(toDateTime('2020-01-01 00:00:00', 'UTC'), 'b');
    select * from my_table where mytime < now();
    select * from my_table where mytime < toDateTime(now(), 'UTC');
    

    在我的服务器上,选择 now() 还是转换它都没有关系。我还尝试了您最初定义表格的方式,并且也可以。因此,您的数据出现问题的想法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-31
      • 2011-06-15
      相关资源
      最近更新 更多