我想在 MySQL 中做一个简单的日期和时间比较。像这样:
IF( NOW() > Ticket.fixTime )
日期采用以下格式
IF(2012-09-25 08:47:25 > 2012-09-24 18:15:16)
但是,这会返回错误的结果。
关于如何实现这一目标的任何建议?
谢谢!
你必须这样:
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)
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 感到困惑IF
,where
你只是想要
SELECT * FROM your_table WHERE NOW() > Ticket.fixTime