【问题标题】:Eloquent MYSQL statement : WHERE NOT(A OR B)雄辩的 MYSQL 语句:WHERE NOT(A OR B)
【发布时间】:2017-08-31 05:07:13
【问题描述】:

我正在研究可以在 13 个肯定条件下编写的日期范围重叠功能,以检查日期间隔是否重叠:

https://en.wikipedia.org/wiki/Allen%27s_interval_algebra

或者在 2 个否定条件下更优雅:

http://baodad.blogspot.nl/2014/06/date-range-overlap.html

在 MYSQL 中:

WHERE NOT(`last_day` <= '2001-06-01' OR `first_day` >= '2022-12-01');

这需要这样的东西,我在文档中找不到:

$query = $query->whereNot(function ($query) use ($first_day, $last_day) {
   $query->where('last_day', '<=', $first_day);
   $query->orWhere('first_day', '<=', $last_day);
});

我该如何解决这个问题?

这与我的其他可能重复的帖子有关:Laravel / Eloquent WHERE NOT SUBQUERY

但我希望我的问题现在更清楚了。

【问题讨论】:

    标签: php laravel laravel-5 eloquent laravel-eloquent


    【解决方案1】:

    您可以通过还原 last_dayfirst_day 的约束来实现所需的 - 这样就无需使用 NOT 子句。

    而不是做

    WHERE NOT(`last_day` <= '2001-06-01' OR `first_day` >= '2022-12-01');
    

    你可以的

    WHERE `last_day` > '2001-06-01' AND `first_day` < '2022-12-01';
    

    使用 Eloquent builder 应该可以解决问题:

    $query = $query->where('last_day', '>', $first_day)->where('first_day', '>', $last_day);
    

    【讨论】:

      猜你喜欢
      • 2014-09-20
      • 2017-10-18
      • 1970-01-01
      • 1970-01-01
      • 2014-11-09
      • 2017-01-30
      • 2016-07-05
      • 2014-01-03
      • 1970-01-01
      相关资源
      最近更新 更多