【问题标题】:SELECT epoch timestamp bigger then my variableSELECT epoch timestamp 比我的变量大
【发布时间】:2012-04-30 10:06:54
【问题描述】:

这是为什么

SELECT strftime('%s', timestamp,'localtime') from locationLog WHERE strftime('%s', timestamp,'localtime') > 999999999999999999 ORDER BY _id DESC

当我的所有行在“时间戳”中的值都较低时,返回任何输出

在我的情况下,上面的查询返回

1334735588
1334735349
1334734317
1334734178
1334734172
and so on...

它返回我的整个表。

如果我将> 切换到<,它不会返回任何内容。

我想我正在尝试比较不同类型的变量或类似的东西。

【问题讨论】:

    标签: php sql sqlite


    【解决方案1】:

    您将text 值与integer 值进行比较,因此sqlite 将integer 值转换为text,并进行字符串比较:

    sqlite> select strftime('%s', '2002-02-02', 'localtime');
    1012611600
    sqlite> select typeof(strftime('%s', '2002-02-02', 'localtime'));
    text
    sqlite> select typeof(999999999999999999);
    integer
    sqlite> select strftime('%s', '2002-02-02', 'localtime') > 999999999999999999;
    1
    sqlite> select cast(strftime('%s', '2002-02-02', 'localtime') as integer) > 999999999999999999;
    0
    

    如上图,解决方法是将strftime的返回值强制转换为某种数值类型。

    【讨论】:

    • 我已经用完全相同的解决方案弄清楚了,但由于我的声誉低下,我只能在几个小时内回答自己。所以你打败了时钟。 (你也教育了我关于 typeof)非常感谢你的回答。
    【解决方案2】:

    猜测一下,您使用的是 32 位平台,并且您使用了大于 2147483647 的整数这一事实导致您遇到了 "Year 2038 Problem" 的变化。

    尝试使用2147483647 作为您在查询中比较的数字。

    这将持续到 2038 年,届时您可能会在 64 位平台上托管您的应用程序(或者到那时甚至可能是 128 位或 256 位!)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-05
      • 2019-05-02
      • 2019-06-19
      • 2012-12-13
      • 1970-01-01
      • 2016-12-18
      • 2021-04-22
      相关资源
      最近更新 更多