MySQL比较DATE字符串与DATETIME字段中的字符串 [英] MySQL compare DATE string with string from DATETIME field

查看:928
本文介绍了MySQL比较DATE字符串与DATETIME字段中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题:是否可以通过比较一个DATE字符串2010-04-29和存储为DATETIME(2010-04-29 10:00)的字符串从MySQL数据库中选择?



我有一个过滤数据的日期选择器,我想通过DATETIME字段查询表格,如下所示:

  SELECT * FROM`calendar` WHERE startTime ='2010-04-29'

...并且我想获取DATETIME值为2010-04-29 10:00的行。



任何建议


解决方案

SELECT * FROM`calendar` WHERE DATE(startTime)='2010-04-29'


$ b b

只是为了参考我有一个200万记录表,我运行类似的查询。
Salils答案花了4.48秒,上面花了2.25秒。



所以如果表是BIG,我会建议这样。


I have a question: Is it possible to select from a MySQL database by comparing one DATE string "2010-04-29" against strings that are stored as DATETIME (2010-04-29 10:00)?

I have one date picker that filters data and I would like to query the table by the DATETIME field like this:

SELECT * FROM `calendar` WHERE startTime = '2010-04-29'"

...and I would like to get the row that has the DATETIME value of "2010-04-29 10:00".

Any suggestions? Thanks.

解决方案

Use the following:

SELECT * FROM `calendar` WHERE DATE(startTime) = '2010-04-29'

Just for reference I have a 2 million record table, I ran a similar query. Salils answer took 4.48 seconds, the above took 2.25 seconds.

So if the table is BIG I would suggest this rather.

这篇关于MySQL比较DATE字符串与DATETIME字段中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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