MySQL 将 DATE 字符串与来自 DATETIME 字段的字符串进行比较

2021-11-20 00:00:00 datetime comparison mysql

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

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

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

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

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

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

有什么建议吗?谢谢.

推荐答案

使用以下内容:

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

仅供参考 我有一个 200 万条记录表,我运行了一个类似的查询.Salils 回答用了 4.48 秒,上面用了 2.25 秒.

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.

相关文章