MySQL 大于微时时间戳 [英] MySQL greater than with microtime timestamp

查看:56
本文介绍了MySQL 大于微时时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 MySQL 数据库中插入行的 PHP 脚本.每行都有一个字段created_at",其中填充了 PHP 函数 microtime(true) 的值,并作为双精度插入.(微时因为我需要比秒更精确的东西)

I have one PHP script inserting rows in a MySQL database. Each row has a field 'created_at' which is filled with the value of the PHP function microtime(true), and inserted as a double. (microtime because I need something more precise than to the second)

我有另一个 PHP 脚本,它根据 created_at 字段选择行.

I have another PHP script that selects rows based on that created_at field.

当我继续选择时:SELECT * FROM `ms_voltage` WHERE created_at >1302775523.51878我收到一个结果集,第一行是 created_at 的正是该值的行.

When I go ahead and select like this: SELECT * FROM `ms_voltage` WHERE created_at > 1302775523.51878 I receive a resultset with, as the first row, the row with exactly that value for created_at.

当手动执行查询时,这会在我的 PHP 脚本和 PhpMyAdmin 中发生.但并非总是如此,并非针对每个值.真的只是偶尔.

This occurs from within my PHP script and from within PhpMyAdmin when manually doing the query. But not always, not for every value. Just once and a while really.

这怎么可能?我没有要求大于/等于,我要严格大于.我是否忽略了一些与类型相关的东西?

How is this possible? I didn't ask for greater than/equals, I want strictly greater than. Am I overlooking something type-related perhaps?

推荐答案

是的,浮点运算有时可以做到这一点.要理解其中的原因,了解一下并非​​所有数字都可以以 10 为基数准确表示,也并非所有数字都可以以 2 为基数准确表示.

Yeah, floating point arithmetic can do that sometimes. To understand why, it's helpful to realize that just as not all numbers can be accurately represented in base 10, not all numbers can be accurately represented in base 2 either.

例如,1/3"可以以 10 为底数写成 0.33333 或 0.33334.两者都不是真正的正确";他们只是我们能做的最好的.以 10 为基数的DOUBLE"可能是 0.3333333333 或 0.3333333334,这是数字的两倍,但仍不正确".

For example, "1/3" may be written in base 10 as 0.33333 or 0.33334. Neither is really "correct"; they're just the best we can do. A "DOUBLE" in base 10 might be 0.3333333333 or 0.3333333334, which is double the digits, yet still not "correct".

最好的选择是使用 DECIMAL 值,或使用 INT 值(并将您的实际值乘以 10000 或 100000 以获得你关心的十进制数字到那个 int 中).

The best options are to either use a DECIMAL value, or use an INT value (and multiply your actual values by, say, 10000 or 100000 in order to get the decimal digits you care about into that int).

这篇关于MySQL 大于微时时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆