我有一个表,其中有一个日期列。我们使用日期范围,也就是说,如果我有一排日期为2022-10-10,下一个是2022-10-15。这意味着第一行的数据也将在中间日子(10-11-12-13-14)采取。
我遇到的问题是我不知道如何正确地执行查询。如果用户选择一个天数范围,它应该获取属于该范围的行。
例如,如果用户选择从2022-10-12到15,他应该收集两个注释行,一个从第10天开始,另一个从第15天开始,但是如果范围是从10到12,他应该只有第10天的行(相同的用户选择从12到13)。
表的例子:
ID range_date data_id price
=== ======= ======= =======
1 2022-10-07 3 10
2 2022-10-10 5 50
3 2022-10-15 5 20
4 2022-10-16 3 40
... ... ...添加一些示例的编辑:
-With date from 2022-10-10 to 2022-10-12 -> return line 2
-With date from 2022-10-11 to 2022-10-13 -> return line 2
-With date from 2022-10-9 to 2022-10-15 -> return lines 1,2,3
-With date from 2022-10-9 to 2022-10-9 -> return line 1发布于 2022-06-09 10:10:45
假设开始和结束筛选日期为,
$startDate = Carbon::parse($request->startDate)->startOfDay();
$endDate = Carbon::parse($request->endDate)->endOfDay();
$fitlerdData = Table::where(function($query) use($startDate, $endDate){
$query->where('date', '>=', $startDate)
->where('date', '<=', $endDate);
})->get();发布于 2022-06-09 10:17:49
您应该使用whereBetween()方法:
// $start = 2022-10-12;
// $end = 2022-10-15;
ModelName::whereBetween('date', [$start, $end])->get();https://stackoverflow.com/questions/72557639
复制相似问题