我想将我的查询转换为 Laravel 构建器格式:
这是我的工作查询:
SELECT * FROM tickets
WHERE tic_status_id = 65
AND (tic_escalation_date1 IS NULL OR tic_escalation_date1 < DATE('2019-06-22 13:00:00'))
AND (tic_escalation_date2 IS NULL OR tic_escalation_date2 < DATE('2019-06-22 13:00:00'))
AND (tic_escalation_date3 IS NULL OR tic_escalation_date3 < DATE('2019-06-22 13:00:00'))
我正在尝试使用 whereNull 和 orWhere 但它不起作用。
最佳答案
您可以使用 Parameter Grouping限制或在哪里。
Passing a Closure into the where method instructs the query builder to begin a constraint group. The Closure will receive a query builder instance which you can use to set the constraints that should be contained within the parenthesis group.
然后,在约束内,您可以使用 whereNull()向查询添加“where null”子句,以及 orWhereDate()向查询添加“或地点日期”语句。注意 whereDate 方法将从 datetime 中取出日期部分表达。
DB::table('tickets')
->where('tic_status_id', '=', 65)
->where(function ($query) {
$query->whereNull('tic_escalation_date1')
->orWhereDate('tic_escalation_date1', '<', '2019-06-22 13:00:00');
})
->where(function ($query) {
$query->whereNull('tic_escalation_date2')
->orWhereDate('tic_escalation_date2', '<', '2019-06-22 13:00:00');
})
->where(function ($query) {
$query->whereNull('tic_escalation_date3')
->orWhereDate('tic_escalation_date3', '<', '2019-06-22 13:00:00');
})
->get();
关于sql - Laravel Builder 中的 1 where 子句中的 2 个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56697003/