我的表中有一个 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/