在我的数据库中,creadted_at数据2017-11-07 18:58:16,2017-11-07 19:58:16
。我尝试使用WhereBetween在2017-11-07日期搜索数据
我不确定如何将Like % %
放入我的查询
'like', '%'.2017-11-07.'%'
->WhereBetween('created_at', ['2017-11-07', '2017-12-07'])
这是我的完整控制器
$b = DB::table("v_dealer_sell_time")
->select([
'product_name',
DB::raw('COALESCE(SUM(total_price), 0) AS total_price'),
DB::raw('COALESCE(SUM(total_product), 0) AS total_product')
])
->WhereBetween('created_at', ['2017-11-07', '2017-11-07'])
->groupBy('product_name')
->get();
答案 0 :(得分:1)
使用whereDate()
:
$date = Carbon::parse($date)->toDateString();
....
->whereDate('created_at', $date)
或whereBetween()
:
$date = Carbon::parse($date);
$from = $date->copy()->startOfDay();
$to = $date->copy()->endOfDay();
....
->whereBetween('created_at', [$from, $to])
答案 1 :(得分:1)
如果你想在同一个给定日期创建的那个,你可以像这样使用whereDate
但是从5.3(Documentation)开始:
$b = DB::table("v_dealer_sell_time")
->select([
'product_name',
DB::raw('COALESCE(SUM(total_price), 0) AS total_price'),
DB::raw('COALESCE(SUM(total_product), 0) AS total_product')
])
->WhereDate('created_at', '=', $date)
->groupBy('product_name')
->get();
如果你想在两个日期之间使用,请使用whereBetween:
$b = DB::table("v_dealer_sell_time")
->select([
'product_name',
DB::raw('COALESCE(SUM(total_price), 0) AS total_price'),
DB::raw('COALESCE(SUM(total_product), 0) AS total_product')
])
->WhereBetween('created_at', [Carbon::parse('2017-11-07')->startOfDay(), Carbon::parse('2017-12-07')->endOfDay()])
->groupBy('product_name')
->get();
PS:不要忘记在控制器顶部添加use Carbon\Carbon;
。
答案 2 :(得分:1)
如果此日期搜索与搜索功能相关,则您必须执行以下操作
<强>代码强>
将此更改为
->WhereBetween('created_at', ['2017-11-07', '2017-11-07'])
此
->WhereBetween('created_at', ['2017-11-07 00:00:00', '2017-11-07 23:59:59'])
为什么?
2017-11-07 00:00:00
- 当天的开始
2017-11-07 23:59:59
- 当天结束
如果你使用
tosql()
并检查你的查询它有类似的东西
.`created_at` between ? and ? "