mysql 日期时间比较

发布于 2024-09-28 15:19:24 字数 172 浏览 10 评论 0原文

例如,以下查询工作正常:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冧九 2024-10-05 15:19:24

...这显然是在执行“字符串”比较

否 - 如果日期/时间格式与支持的格式匹配,MySQL 会根据要比较的列执行隐式转换,将值转换为 DATETIME。同样的情况也发生在:

WHERE int_column = '1'

...其中字符串值“1”被转换为 INTeger,因为 int_column 的数据类型是 INT,而不是 CHAR/VARCHAR/TEXT。

如果要将字符串显式转换为 DATETIME,则 STR_TO_DATE 函数 将是最佳选择:

WHERE expires_at <= STR_TO_DATE('2010-10-15 10:00:00', '%Y-%m-%d %H:%i:%s')

...this is obviously performing a 'string' comparison

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 int_column = '1'

...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:

WHERE expires_at <= STR_TO_DATE('2010-10-15 10:00:00', '%Y-%m-%d %H:%i:%s')
贱人配狗天长地久 2024-10-05 15:19:24

但这显然是在执行“字符串”比较

。字符串将自动转换为 DATETIME 值。

请参阅11.2。表达式求值中的类型转换。

当运算符与不同类型的操作数一起使用时,会发生类型转换以使操作数兼容。有些转换是隐式发生的。例如,MySQL 根据需要自动将数字转换为字符串,反之亦然。

But this is obviously performing a 'string' comparison

No. The string will be automatically cast into a DATETIME value.

See 11.2. Type Conversion in Expression Evaluation.

When an operator is used with operands of different types, type conversion occurs to make the operands compatible. Some conversions occur implicitly. For example, MySQL automatically converts numbers to strings as necessary, and vice versa.

素罗衫 2024-10-05 15:19:24

我知道它已经很老了,但我刚刚遇到了这个问题,并且我在 SQL 文档中看到了以下内容:

[为了在将 BETWEEN 与日期或时间值一起使用时获得最佳结果,]使用 CAST() 将值显式转换为所需的数据类型。示例:如果将 DATETIME 与两个 DATE 值进行比较,请将 DATE 值转换为 DATETIME 值。如果在与 DATE 的比较中使用字符串常量(例如“2001-1-1”),请将字符串转换为 DATE。

我认为最好使用 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 :

[For best results when using BETWEEN with date or time values,] use CAST() to explicitly convert the values to the desired data type. Examples: If you compare a DATETIME to two DATE values, convert the DATE values to DATETIME values. If you use a string constant such as '2001-1-1' in a comparison to a DATE, cast the string to a DATE.

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文