首页 > 解决方案 > 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

表规格:

我遇到了性能问题,并认为这可能是由DATE_SUBWHERE 子句中的函数引起的。我对这里的研究几个月来只产生了 Date_Sub 作为减法方法。

有没有更有效的方法来编写语句?

任何帮助是极大的赞赏。

标签: mysqldatabasetime

解决方案


推荐阅读