mysql 日期时间比较
例如,以下查询工作正常:
SELECT *
FROM quotes
WHERE expires_at <= '2010-10-15 10:00:00'
但这显然是在执行“字符串”比较 - 我想知道 MySQL 中是否有一个内置函数专门进行“日期时间”比较。
For example the following query works fine:
SELECT *
FROM quotes
WHERE expires_at <= '2010-10-15 10:00:00'
But this is obviously performing a 'string' comparison - I was wondering if there was a function built in to MySQL that specifically does 'datetime' comparisons.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
否 - 如果日期/时间格式与支持的格式匹配,MySQL 会根据要比较的列执行隐式转换,将值转换为 DATETIME。同样的情况也发生在:
...其中字符串值“1”被转换为 INTeger,因为
int_column
的数据类型是 INT,而不是 CHAR/VARCHAR/TEXT。如果要将字符串显式转换为 DATETIME,则 STR_TO_DATE 函数 将是最佳选择:
No - if the date/time format matches the supported format, MySQL performs implicit conversion to convert the value to a DATETIME, based on the column it is being compared to. Same thing happens with:
...where the string value of "1" is converted to an INTeger because
int_column
's data type is INT, not CHAR/VARCHAR/TEXT.If you want to explicitly convert the string to a DATETIME, the STR_TO_DATE function would be the best choice:
。字符串将自动转换为 DATETIME 值。
请参阅11.2。表达式求值中的类型转换。
No. The string will be automatically cast into a DATETIME value.
See 11.2. Type Conversion in Expression Evaluation.
我知道它已经很老了,但我刚刚遇到了这个问题,并且我在 SQL 文档中看到了以下内容:
我认为最好使用 STR_TO_DATE 因为他们花时间为此创建了一个函数,而且我在 BETWEEN 文档中找到了这个函数......
I know its pretty old but I just encounter the problem and there is what I saw in the SQL doc :
I assume it's better to use STR_TO_DATE since they took the time to make a function just for that and also the fact that i found this in the BETWEEN doc...