sql - Laravel Builder 中的 1 where 子句中的 2 个条件

标签 sql laravel laravel-5 eloquent

我想将我的查询转换为 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/

相关文章:

php - 数据库设计: Multiple User Entities. 单个或多个表

php - 如何使用具有日期条件的其他表减去整数数据?

php - 如何在 Laravel 中获取 session ID?

php - Laravel Response::download()函数图像问题

MySQL 根据另一个表的结果批量更新新表

php - 每 30 分钟计算一次数据库中的重复记录

sql - 忽略 postgresql rank() 窗口函数中的空值

php - Laravel - 自定义请求中的关联数组验证

php - 有限制地排队 Guzzle 请求

php - Laravel 删除可能有错误关系的多态关系