mysql datetime比较 [英] mysql datetime comparison

查看:1614
本文介绍了mysql datetime比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For example the following query works fine:

SELECT * 
  FROM quotes 
 WHERE expires_at <= '2010-10-15 10:00:00';

但这显然是在执行字符串"比较-我想知道MySQL是否内置了专门用于日期时间"比较的函数.

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.

推荐答案

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

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

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

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'

...由于"int_column"的数据类型是INT,而不是CHAR/VARCHAR/TEXT,因此将字符串值"1"转换为INTeger.

...where the string value of "1" is converted to an INTeger because int_column's data type is INT, not CHAR/VARCHAR/TEXT.

如果要将字符串显式转换为DATETIME,请

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')

这篇关于mysql datetime比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆