gpt4 book ai didi

mysql - 什么更好 : select date with trunc date or between

转载 作者:行者123 更新时间:2023-11-29 02:25:11 25 4
gpt4 key购买 nike

我需要创建一个查询来根据日期选择我的 mysql 数据库的一些数据,但是在我的 where 子句中我必须选择:

1 - 截断日期:

select count(*) from mailing_user where date_format(create_date, '%Y-%m-%d')='2013-11-05';

2 - 在

之间使用
select count(*) from mailing_user where create_date between '2013-11-05 00:00:00' and '2013-11-05 23:59:59';

这两个查询都可以,但是哪个更好呢?或者,有什么推荐的?为什么?

最佳答案

这是一篇要阅读的文章。

http://willem.stuursma.name/2009/01/09/mysql-performance-with-date-functions/

如果你的 created_date 列被索引,第二个查询会更快。

但是如果该列没有被索引并且这是你定义的日期格式,你可以使用下面的查询。

select count(*) from mailing_user where DATE(create_date) = '2013-11-05';

我使用 DATE 而不是 DATE_FORMAT,因为我可以利用获取这种格式 ('2013-11-05') 的 native 功能。

关于mysql - 什么更好 : select date with trunc date or between,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23658183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com