MySQL compare DATE string with string from DATETIME field(MySQL 将 DATE 字符串与来自 DATETIME 字段的字符串进行比较)
问题描述
我有一个问题:是否可以通过将一个 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.
这篇关于MySQL 将 DATE 字符串与来自 DATETIME 字段的字符串进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL 将 DATE 字符串与来自 DATETIME 字段的字符串进行比较


基础教程推荐
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01