1

我想在 MySQL 中做一个简单的日期和时间比较。像这样:

IF( NOW() > Ticket.fixTime )日期采用以下格式

IF(2012-09-25 08:47:25  >  2012-09-24 18:15:16)

但是,这会返回错误的结果。

关于如何实现这一目标的任何建议?

谢谢!

4

2 回答 2

1

你必须这样:

IF( TO_SECONDS(NOW()) > TO_SECONDS(Ticket.fixTime) )

解释

您可以使用TO_SECONDS将时间转换为秒并进行比较。

IF('TO_SECONDS(2012-09-25 08:47:25') > TO_SECONDS('2012-09-24 18:15:16'))

输出

输出是binary,即,要么1要么0

mysql> SELECT TO_SECONDS('2012-09-25 08:47:25') > TO_SECONDS('2012-09-24 18:15:16');
+-----------------------------------------------------------------------+
| TO_SECONDS('2012-09-25 08:47:25') > TO_SECONDS('2012-09-24 18:15:16') |
+-----------------------------------------------------------------------+
|                                                                     1 |
+-----------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT TO_SECONDS('2012-09-25 08:47:25') < TO_SECONDS('2012-09-24 18:15:16');
+-----------------------------------------------------------------------+
| TO_SECONDS('2012-09-25 08:47:25') < TO_SECONDS('2012-09-24 18:15:16') |
+-----------------------------------------------------------------------+
|                                                                     0 |
+-----------------------------------------------------------------------+
1 row in set (0.00 sec)

IF健康)状况

IF条件应该这样使用:

mysql> SELECT IF (TO_SECONDS('2012-09-25 08:47:25') < TO_SECONDS('2012-09-24 18:15:16'), 'True', 'False');
+---------------------------------------------------------------------------------------------+
| IF (TO_SECONDS('2012-09-25 08:47:25') < TO_SECONDS('2012-09-24 18:15:16'), 'True', 'False') |
+---------------------------------------------------------------------------------------------+
| False                                                                                       |
+---------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT IF (TO_SECONDS('2012-09-25 08:47:25') > TO_SECONDS('2012-09-24 18:15:16'), 'True', 'False');
+---------------------------------------------------------------------------------------------+
| IF (TO_SECONDS('2012-09-25 08:47:25') > TO_SECONDS('2012-09-24 18:15:16'), 'True', 'False') |
+---------------------------------------------------------------------------------------------+
| True                                                                                        |
+---------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
于 2012-09-25T07:58:59.863 回答
0

Mysql IF 语句用作

IF(condition, valueA, valueB) // valueA for condition is true otherwise valueB

所以你应该像这样使用它:

SELECT IF(NOW() > Ticket.fixTime, 'what you want to show', 'i do not know')
FROM your_table

或者你对 and 感到困惑IFwhere你只是想要

SELECT * FROM your_table WHERE NOW() > Ticket.fixTime
于 2012-09-25T08:00:37.697 回答