在我的数据库中 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'])
这是我的完整 Controller
$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();
最佳答案
如果你想在相同的给定日期创建的,你可以像这样使用 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:不要忘记在 Controller 顶部添加 use Carbon\Carbon;
。
关于php - 如何在 sql 查询中将 WhereBetween 日期与 %LIKE% 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46813739/