PHP Laravel - MSSQL - WhereBetween和Where?

问题描述 投票:0回答:1

我试图从我的MSSQL数据库中选择记录,参数如下。

  • "first_etd "列必须在两个日期之间。
  • 列'car_id'必须与当前的'car_id'不一样。$carId 变量。

这是我的代码。

$carId = 47;
$from = Carbon::now()->subWeeks(2)->startOfWeek()->toDateString();
$to = Carbon::now()->addWeeks(4)->endOfWeek()->toDateString();

$consols = Consol::with(['car'])
                   ->where('car_id', '!=', $carId)
                   ->whereBetween('first_etd', [$from, $to])->get();

上面的变量 $consols 不返回任何结果。

如果我删除 ->where('car_id', '!=', $carId) 的语句,我成功地得到了结果。

我的数据库中的所有记录目前都有 NULLcar_id 列。

我也试着改了一下 != 经营者 <> 没有任何运气。

php sql-server laravel
1个回答
0
投票

所以这不是对SQL语句本身的修复,更多的是对我的问题的修复。

正如我在上一条中所说的,如果我去掉下面的 where() 的方法。我想我可以先把记录弄到手,也就是在我的两个日期之间,然后再做。where() 对结果集合进行过滤。

这样就可以了。

//Get the records from the database, that's between two dates.
$consols = Consol::with(['car'])->whereBetween('first_etd', [$from, $to])->get();

//Now $consols is a Laravel collection, so I can use the where() method here.
$consols = $this->consols->where('car_id', '!=', $carId);

上面成功返回了两条记录

© www.soinside.com 2019 - 2024. All rights reserved.