mysql - 比较sql server中的日期时间

标签 mysql datetime

我遇到了一个小问题..

在我的数据库中,我有一个包含 2 个日期时间的表..

TABLE1 将 turn_time 作为日期时间,其值为 0000-00-00 23:59:59

TABLE2 的 last_move 日期时间值为 2013-09-23 14:20:19

我想用 SQL 计算如果turn_time + last_move < NOW()。

但是我得到的结果看起来像这样: 40261846333938.000000

如果turn_time + last_move < NOW(),我如何获得当前行的简单选择?

我的 SQL:

SELECT * FROM active_games 
        INNER JOIN games 
        ON active_games.fk_games_id = games.games_id 
        INNER JOIN product
        ON active_games.fk_product = product.product_id
        INNER JOIN users
        ON active_games.fk_user_id_turn = users.user_id
        INNER JOIN user_profile 
        ON users.user_id = user_profile.fk_user_id
        WHERE turn_time + last_move < NOW()

感谢您的宝贵时间。

最佳答案

您可以尝试使用此 WHERE 子句:

WHERE UNIX_TIMESTAMP(turn_time) + UNIX_TIMESTAMP(last_move) < UNIX_TIMESTAMP(NOW())

NOW() 的文档说如下:

Returns the current date and time 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.

由于 NOW()WHERE 子句中的数字上下文中使用,因此它返回数字值(在 YYYYMMDDHHMMSS.uuuuuu > 格式),并尝试将它们与 DateTime turn_time + last_move 进行比较。将所有内容转换为 Unix 时间戳应该可以使比较正常进行。

希望这有帮助!

关于mysql - 比较sql server中的日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18964947/

相关文章:

mysql - MySql 中条件的 LIMIT

c# - MS Access 存储的 DateTime.MinValue 不正确?

javascript - 日期比较无法正常工作

php - 如何优化这个 mysql 查询?脚本发布

java - 由于 MySQLSyntaxErrorException : Unknown database 'dbname' error,无法通过 hibernate 连接到 MySQL 数据库

mysql - 优化sql查询,加快搜索速度,目前搜索时间约为85秒

mysql - 避免列中重复数据

javascript - 如何仅在 C# asp.net 中将字符串转换为日期

android - 从 Google Calendar Api DateTime 中分离并分别获取日期和时间

mysql - 如何更新日期时间MySQL中的空时间