使用subtime()查询意外地不返回任何行

时间:2011-08-01 19:29:17

标签: mysql datetime time

我正在使用以下查询:

 SELECT message,timestamp 
 FROM botlogs.tbllogs
 WHERE message like '%Failed to grab car amount 
       or 0 website%' and timestamp > Subtime('2011-08-01 13:20','0:60')

虽然我在2011年8月1日下午1:21:53有一行,但上面的查询不会返回任何行。那是为什么?

如果我跑

select timestamp,message 
from botlogs.tbllogs 
where message like Failed to grab car amount 
       or 0 website%' and timestamp < '2011-08-01 13:20' - INTERVAL 180 SECOND

它从7/31/2011 9:27:24 pm返回错误。

2 个答案:

答案 0 :(得分:2)

由于:

mysql> SELECT Subtime('2011-08-01 13:20','0:60');
+------------------------------------+
| Subtime('2011-08-01 13:20','0:60') |
+------------------------------------+
| NULL                               |
+------------------------------------+
1 row in set, 1 warning (0.04 sec)

试试这个

mysql> SELECT Subtime('2011-08-01 13:20','00:01:00');
+----------------------------------------+
| Subtime('2011-08-01 13:20','00:01:00') |
+----------------------------------------+
| 2011-08-01 13:19:00                    |
+----------------------------------------+
1 row in set (0.00 sec)

或者

mysql> SELECT '2011-08-01 13:20' - INTERVAL 60 SECOND;
+-----------------------------------------+
| '2011-08-01 13:20' - INTERVAL 60 SECOND |
+-----------------------------------------+
| 2011-08-01 13:19:00                     |
+-----------------------------------------+
1 row in set (0.01 sec)

答案 1 :(得分:1)

来自docs

  
      
  • SUBTIME(表达式1,表达式2)
  •   
     

SUBTIME()返回expr1 - expr2,表示为相同的值   格式为expr1。 expr1是时间或日期时间表达式,expr2是   时间表达。

(强调我的)

问题是0:60不是有效的时间表达式:

mysql> select time('0:60');
+--------------+
| time('0:60') |
+--------------+
| NULL         |
+--------------+
1 row in set, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+----------------------------------------+
| Level   | Code | Message                                |
+---------+------+----------------------------------------+
| Warning | 1292 | Truncated incorrect time value: '0:60' |
+---------+------+----------------------------------------+
1 row in set (0.00 sec)

所以减法返回NULL

mysql> select subtime(now(), '0:60');
+------------------------+
| subtime(now(), '0:60') |
+------------------------+
| NULL                   |
+------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+----------------------------------------+
| Level   | Code | Message                                |
+---------+------+----------------------------------------+
| Warning | 1292 | Truncated incorrect time value: '0:60' |
+---------+------+----------------------------------------+
1 row in set (0.00 sec)

NULL导致你的比较为每种可能性返回false,这导致最终结果中的0行。