【问题标题】:SQL Query in Laravel is not returning the correct resultLaravel 中的 SQL 查询没有返回正确的结果
【发布时间】:2021-10-23 14:06:37
【问题描述】:

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

样本数据:

id from_date_1 to_date_1 from_date_2 to_date_2 from_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 NULL NULL
3 2021-06-25 2021-08-20 NULL NULL NULL NULL

我的 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);
                    
                }
            }

结果应该是:

id from_date_1 to_date_1 from_date_2 to_date_2 from_date_3 to_date_3
3 2021-06-25 2021-08-20 NULL NULL NULL NULL

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

【问题讨论】:

  • 所以你想通过查询来实现这一点,而不是每个都是它
  • 不,我可以使用 foreach,最重要的是获得我提供的正确结果。谢谢
  • @OsamaShaki 不清楚问题出在哪里。 Nipun 有一点,我不认为 foreach 是处理这个问题的好方法。您可以将 if 语句放在您的视图中,而不是创建一个新集合,您可以在其中显示表格,或者您可以编辑您的选择以仅获取您真正需要的内容。更改查询是最好的方法。
  • @NipunTharuksha 根据可用时段获取过期列表,因为用户可能不会提供 3 个时段,第 1 个时段是必须的。谢谢
  • @OsamaShaki 对我来说,目前还不清楚正确的数据是什么。所以很难帮你查询。我同意不要使观点过于复杂。

标签: sql laravel


【解决方案1】:

我已经重新创建了您的数据库并使用上述数据测试了下面的代码。不需要使用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();

数据库

查询

$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
  }
]

效率

更新

由于要求的答案后来已被修改,我的答案已更新 查询如下

$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
  }
]

效率

【讨论】:

  • Nipun,我应该只从样本数据中获取第三行。不记录1&2...你可以参考结果样本。谢谢
  • 是的,我只修改结果。仅当该行的最新非空 to_date_* 早于现在时,才应返回该行。希望现在很清楚。非常感谢
  • 嗨@Nipun,您能否更新查询以获取示例结果?谢谢
  • @OsamaShaki 请检查
  • 非常感谢 Nipun,感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-29
相关资源
最近更新 更多