mysql - Mysql 日期比较中的异常结果

标签 mysql date

我的表中有一个 datetime 数据类型,

 `DateAdded` datetime(4) DEFAULT NULL,

我的数据库中有一条记录,其值 DateAdded = 2017-09-11 17:02:48.6531(其 ID = 16452994)。

当我想通过以下查询返回 NULL 来获取它时

select `ID`,`DateAdded` from `Add` where   `DateAdded` <= FROM_UNIXTIME(('1505071799' +86400 ), '%Y-%m-%d %h:%i:%s') and ID =16452994 ;

仅供引用:FROM_UNIXTIME(('1505071799' +86400 ), '%Y-%m-%d %h:%i:%s') = 2017-09-11 23:59:59

奇怪的是2017-09-11 17:02:48.6531 <= 2017-09-11 23:59:59返回false

但是当我尝试以下查询时,我会得到我想要的结果。

select `ID`,`DateAdded` from `Add` where   `DateAdded` <= FROM_UNIXTIME(('1505071799' +86400 +1 ), '%Y-%m-%d %h:%i:%s') and ID =1645299; 

我想知道为什么会发生这种情况以及如何解决这个问题?

最佳答案

FROM_UNIXTIME(unix_timestamp), FROM_UNIXTIME(unix_timestamp,format)

Returns a representation of the unix_timestamp argument as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context. The value is expressed in the current time zone. unix_timestamp is an internal timestamp value such as is produced by the UNIX_TIMESTAMP() function.

If format is given, the result is formatted according to the format string, which is used the same way as listed in the entry for the DATE_FORMAT() function.

https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime

关于mysql - Mysql 日期比较中的异常结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46275687/

相关文章:

MySQL 数据库结构帮助

由于时区问题,Java 应用程序无法执行

javascript - Firefox new Date() 从字符串构造本地时区时间

java - 检查 String 是否可以被 SimpleDateFormat 解析

php - 发送 : Fetching row from session db table after generating session id

php - 使用 MySQL 和 PHP 计算字段中每个值的数量

mysql - 更新Where字段是最小的数字

php - 如何使用 PHP Codeigniter 将 MySQL 数据库中的数字显示为星号

带有冒号分隔符的时区的Java SimpleDateFormat?

android - 在 Android 中按日期对数组列表项进行分组