mysql - 从下周获得物品
问题描述
我开始做一个查询,例如:
SELECT *
FROM mytable
WHERE (weekofyear(now())+1) <= date_inserted < weekofyear(now()+2)
这样做的问题是,前几年的项目也会出现在这里。执行此查询的最佳方法是什么-我希望我仍然可以在date_inserted
字段上的查询中使用索引,这就是我在这里问这个的原因
解决方案
我将其表述为:
where
date_inserted >= current_date + interval 7 - weekday(current_date) day
and date_inserted < current_date + interval 14 - weekday(current_date) day
表达式current_date + interval 7 - weekday(current_date) day
为您提供下周的第一天。这在假设您的周从星期一开始的假设下有效。
您可以看到列上没有应用日期函数date_inserted
,这意味着 MySQL 很乐意使用该列上的索引进行过滤(如果有的话)。
推荐阅读
- assembly - 为什么要保存某些寄存器?如果没有,会出什么问题?
- java - 如何在 Java 中将布尔类型方法转换为 void 方法?
- php - 计算没有列分配的 PHP 数组的特定值
- amazon-web-services - 尝试通过多处理从 s3 读取图像时遇到问题
- javascript - Angular:(重新)激活谷歌分析(用户同意所有cookies后的cookies
- google-apps-script - 自动突出显示 Google Docs 中某些单词的脚本?
- javascript - MUI createTheme 未正确将主题传递给 MUI 组件
- qt - ContainsMouse 在父更改上给出了不正确的值
- javascript - 如何创建公司团队和名称列表的二维数组,以便循环遍历它?
- c++ - 有没有办法优化/减少这个分配变量的顺序?(C++)