使用with解决mysql日期匹配及随机月份问题
原始sql遇到诡异情况,查询不到预期的随机月份数据。解决这个问题,mysql 8提供了一个方法:使用with语句。
with mo1 as (select date_format(date_add('2023-11-01', interval floor(rand() * datediff(curdate(), '2023-11-01')) day), '%y-%m') as month) select * from teacher join mo1 on mo1.month = date_format(create_time, '%y-%m')
然而,更好的解决方案是:
select * from teacher where create_time between '2024-01-01 00:00:00' and '2024-01-31 23:59:59'
alter table teacher add index (create_time);
通过这些优化,可以有效解决mysql中的日期匹配和随机月份问题,提高查询性能。
以上就是MySQL日期匹配:如何解决随机月份数据查询问题?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号