雄辩的MYSQL语句:WHERE NOT(A或B) [英] Eloquent MYSQL statement : WHERE NOT(A OR B)
问题描述
我正在处理日期范围重叠功能,可以在13个正面条件下编写,以检查日期间隔是否重叠:
I`m working on date-range-overlap functionality that can be written in 13 positive conditions to check if the date intervals overlap :
https://en.wikipedia.org/wiki/Allen%27s_interval_algebra
或更优雅的2个负面条件:
Or more elegantly in 2 negative conditions:
http://baodad.blogspot.nl/2014/06/date-range-overlap.html
在MYSQL中:
WHERE NOT(`last_day` <= '2001-06-01' OR `first_day` >= '2022-12-01');
这将需要这样的东西,我可以在文档中找到:
This would require something like this, which I can`t find in the docs:
$query = $query->whereNot(function ($query) use ($first_day, $last_day) {
$query->where('last_day', '<=', $first_day);
$query->orWhere('first_day', '<=', $last_day);
});
我该如何解决?
这与我其他可能重复的帖子有关: Laravel / Enloft在哪里不附加
This is related to my other possibly duplicate post : Laravel / Eloquent WHERE NOT SUBQUERY
但我希望我的问题现在更清楚。
But I hope my question is more clear now.
推荐答案
通过恢复 last_day 和 first_day 之间的约束来实现所需要的 - 这样就不需要使用 NOT 子句。
You can achieve what you need by reverting the constraints on last_day and first_day - this way there is no need to use NOT clause.
而不是
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';
与雄辩的构建者,以下应该做的诀窍:
And with Eloquent builder the following should do the trick:
$query = $query->where('last_day', '>', $first_day)->where('first_day', '>', $last_day);
这篇关于雄辩的MYSQL语句:WHERE NOT(A或B)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!