Laravel 中的 SQL 查询未返回正确的结果

标签 sql laravel

我正在尝试根据可用期获取过期列表,有 3 个不同的可用期。只有当该行的最新非空 to_date_* 早于现在时,才应返回该行。

示例数据:

<表类="s-表"> <头> id 从_date_1 to_date_1 从_date_2 to_date_2 从_date_3 to_date_3 <正文> 1 2021-06-10 2021-08-15 2021-08-16 2021-08-31 2021-09-01 2021-09-15 2 2021-06-25 2021-08-10 2021-08-11 2021-08-25 空 空 3 2021-06-25 2021-08-20 空 空 空 空

我的 SQL 查询是:

$listings = collect();
            $all_listings = Listing::query()
                        ->where('vendor_id', $vendor->id)
                        ->where('is_deleted', 0)
                        ->where('is_published', 1)
                        ->where('is_approved', 1)
                        ->where('lease_term', '!=', 'long_term')
                        ->orderBy('created_at', 'desc')
                        ->paginate(10);

            foreach($all_listings as $lis)
            {
                if($lis->to_date_3 != null && ($lis->to_date_3 < \Carbon\Carbon::now()->format('Y-m-d')))
                {
                    $listings->add($lis);
                    continue;
                }
                elseif($lis->to_date_2 != null && ($lis->to_date_2 < \Carbon\Carbon::now()->format('Y-m-d')))
                {
                    $listings->add($lis);
                    continue;
                }
                elseif($lis->to_date_1 < \Carbon\Carbon::now()->format('Y-m-d'))
                {
                    $listings->add($lis);
                    
                }
            }

结果应该是:

<表类="s-表"> <头> id 从_date_1 to_date_1 从_date_2 to_date_2 从_date_3 to_date_3 <正文> 3 2021-06-25 2021-08-20 空 空 空 空

但是,查询返回所有 3 个列表。如何修复查询以获得正确的结果?

最佳答案

我已经重新创建了你的数据库并用上面的数据测试了下面的代码。不需要使用 fooreach 然后检查这个

迁移

 $table->dateTime('from_date_1');
            $table->dateTime('to_date_1');
            $table->dateTime('from_date_2')->nullable();
            $table->dateTime('to_date_2')->nullable();
            $table->dateTime('from_date_3')->nullable();
            $table->dateTime('to_date_3')->nullable();

数据库

enter image description here

查询

$data = Listing::where(function($query){
            $query->whereDate('to_date_1', '>=', Carbon::now())
                ->orWhereDate('to_date_2', '>=', Carbon::now())
                ->orWhereDate('to_date_3', '>=', Carbon::now());
        })->get();

结果

[
  {
    "id": 1,
    "from_date_1": "2021-06-10 00:00:00",
    "to_date_1": "2021-08-15 00:00:00",
    "from_date_2": "2021-08-16 00:00:00",
    "to_date_2": "2021-08-31 00:00:00",
    "from_date_3": "2021-09-01 00:00:00",
    "to_date_3": "2021-09-15 00:00:00",
    "created_at": null,
    "updated_at": null
  },
  {
    "id": 2,
    "from_date_1": "2021-06-25 00:00:00",
    "to_date_1": "2021-08-10 00:00:00",
    "from_date_2": "2021-08-11 00:00:00",
    "to_date_2": "2021-08-25 00:00:00",
    "from_date_3": null,
    "to_date_3": null,
    "created_at": null,
    "updated_at": null
  }
]

效率

enter image description here

更新

Since required answer has been amended later my answer I have updated the query as follow

$to_date_3 = Listing::whereNotNull('to_date_1')->whereNotNull('to_date_2')->whereDate('to_date_3', '<=', Carbon::now())->get();
$to_date_2 = Listing::whereNull('to_date_3')->whereNotNull('to_date_2')->whereDate('to_date_2', '<=', Carbon::now())->get();
$to_date_1 = Listing::whereNull('to_date_3')->whereNull('to_date_2')->whereDate('to_date_1', '<=', Carbon::now())->get();

$result = $to_date_1->merge($to_date_2)->merge($to_date_3);

结果

[
  {
    "id": 3,
    "from_date_1": "2021-06-25 00:00:00",
    "to_date_1": "2021-08-20 00:00:00",
    "from_date_2": null,
    "to_date_2": null,
    "from_date_3": null,
    "to_date_3": null,
    "created_at": null,
    "updated_at": null
  }
]

效率

enter image description here

关于Laravel 中的 SQL 查询未返回正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68888939/

相关文章:

mysql - 为什么 MySQL 中 (x IS NULL != y IS NULL) 需要额外的括号?

Laravel Redis 缓存

sql - 根据 smallint 和 datetime2(7) 的组合计算年份

python - 如何使用 SQLAlchemy 和 contains_eager 中关系上定义的 order_by?

javascript - 从 PHP7 调用 Mongodb 存储函数

php - 查询生成器上的未定义关系方法 - Laravel

php - jQuery 多重选择显示隐藏不适用于所有选择元素

php - Laravel 二级关系查询

php - 如何构建此 SQL 查询。合并多个表?

mysql - 从表中获取某个日期的上次记录时间和出现次数以及上次原因