mysql - mySQL:从日期中减去月份并在 WHERE 子句中用作条件
问题描述
使用以下代码,我想确定是否对于发生中断的位置 x,是否在六个月前的时间范围内发布了警告声明。
在 where 子句中,我使用Date_Sub
函数结果作为条件:
SELECT disruption.disruptionDate, warning.warningID, warning.warningDate
FROM disruption
JOIN warning ON disruption.locationId = warning.locationID
WHERE warning.warningDate <= disruption.disruptionDate
AND warning.warningDate. >= DATE_SUB(disruption.disruptionDate, INTERVAL 6 MONTH)
ORDER BY disruption.disruptionDate
表规格:
- 警告表:44,814 条记录
- 中断表:962,529 条记录
我遇到了性能问题,并认为这可能是由DATE_SUB
WHERE 子句中的函数引起的。我对这里的研究几个月来只产生了 Date_Sub 作为减法方法。
有没有更有效的方法来编写语句?
任何帮助是极大的赞赏。
解决方案
推荐阅读
- macos - 在 Catalina (kCGWindowName) 中启用屏幕录制 API
- node.js - 我在 ibm 云平台上查找 node.js 的 SDK 时遇到问题,它是否已弃用或重命名?
- javascript - 如何正确检查所有复选框并使用 Vue 更改状态?
- svg - 甚至可以在网格上加载/渲染 SVG 文件吗?
- html - 为什么当我将悬停与 div 一起使用时,它也会悬停在我的文本旁边?
- python-3.x - 使用 python 列表生成输出
- javascript - ReactJS:(大)子组件上的 onClick 处理程序不起作用
- flutter - .forEach 在 Flutter 中接受多行
- javascript - 打开 d3 视觉到特定位置(冰柱)
- flutter - 如何避免动画时底部溢出 - Flutter