mysql 将字符串 'now()' 视为日期时间?
mysql treats the string 'now()' as datetime?
我有一个错误构造的查询,将函数 'now()' 作为字符串包含在内:
select Listing.id,Listing.reactivated FROM `listings` AS Listing WHERE Listing.reactivated < 'now()';
但实际上此查询能够 return 使用 mysql 5.5:
更正结果
| 26662 | 2007-06-04 21:42:51 |
| 26663 | 2007-06-04 21:46:34 |
实际上,有几个日期函数,即使没有括号也能工作;像 'now' 或 'curdate',但是一个简单的 "select 'now()';" 将 return 一个字符串。
实际上,当我们将 Mysql 升级到 MariaDB 10.1 时,我注意到了这一点,它停止工作,上面的查询结果是一个空集,并警告:
| Warning | 1292 | Incorrect datetime value: 'NOW()' |
我知道我们应该修复查询 :),但我想问一下是否有人知道这背后的原因,何时更改,以及是否可以配置此行为?
Type Conversion in Expression Evaluation 声明的文档:
If one of the arguments is a TIMESTAMP
or DATETIME
column and the other argument is a constant, the constant is converted to a timestamp before the comparison is performed.
但这似乎不是这种情况下发生的情况,所以它可能是一个错误。它似乎正在将 DATETIME
转换为字符串,并将其与常量进行比较。因此,如果当前日期时间是 2016-07-19 15:13:06
,您将得到以下结果:
SELECT '2016-07-19 15:13:06' < 'now()';
由于数字在整理顺序中低于字母,因此 returns 1
.
我通过尝试其他字符串得出这个结论:
SELECT NOW() < '300';
这也是returns1
。但是如果我把它改成:
SELECT NOW() < '100', NOW() < '!';
结果是0
。
不要使用引号:'NOW()'
;简单地说 NOW()
.
前者是字符串,后者不是有效的日期时间。 (同上 'NOW'
和 'XYZ'
。)
我有一个错误构造的查询,将函数 'now()' 作为字符串包含在内:
select Listing.id,Listing.reactivated FROM `listings` AS Listing WHERE Listing.reactivated < 'now()';
但实际上此查询能够 return 使用 mysql 5.5:
更正结果| 26662 | 2007-06-04 21:42:51 |
| 26663 | 2007-06-04 21:46:34 |
实际上,有几个日期函数,即使没有括号也能工作;像 'now' 或 'curdate',但是一个简单的 "select 'now()';" 将 return 一个字符串。
实际上,当我们将 Mysql 升级到 MariaDB 10.1 时,我注意到了这一点,它停止工作,上面的查询结果是一个空集,并警告:
| Warning | 1292 | Incorrect datetime value: 'NOW()' |
我知道我们应该修复查询 :),但我想问一下是否有人知道这背后的原因,何时更改,以及是否可以配置此行为?
Type Conversion in Expression Evaluation 声明的文档:
If one of the arguments is a
TIMESTAMP
orDATETIME
column and the other argument is a constant, the constant is converted to a timestamp before the comparison is performed.
但这似乎不是这种情况下发生的情况,所以它可能是一个错误。它似乎正在将 DATETIME
转换为字符串,并将其与常量进行比较。因此,如果当前日期时间是 2016-07-19 15:13:06
,您将得到以下结果:
SELECT '2016-07-19 15:13:06' < 'now()';
由于数字在整理顺序中低于字母,因此 returns 1
.
我通过尝试其他字符串得出这个结论:
SELECT NOW() < '300';
这也是returns1
。但是如果我把它改成:
SELECT NOW() < '100', NOW() < '!';
结果是0
。
不要使用引号:'NOW()'
;简单地说 NOW()
.
前者是字符串,后者不是有效的日期时间。 (同上 'NOW'
和 'XYZ'
。)