两个日期之间的laravel查询错误语法错误

时间:2017-10-10 09:37:40

标签: mysql sql laravel laravel-5.4 laravel-query-builder

请在我的查询中帮助我。我目前收到查询错误。我是laravel的新手,我不知道如何对laravel查询进行查询。这是我的疑问:

$date1 = date("Y-m-d", strtotime($request->datepicker));
        $date2 = date("Y-m-d", strtotime($request->datepicker1));

        $products =  DB::table('shipping_table')
        ->select('products.product_name', 'products.price', DB::raw('Sum(shipping_products.quantity) AS qtysold'), 'shipping_table.sold_date')
        ->join('shipping_products','shipping_table.shipping_id', '=', 'shipping_products.shipping_id')
        ->join('products','products.product_id', '=', 'shipping_products.product_id')
        ->where(['shipping_table.shipping_status', '=' ,1])
        ->whereBetween(DB::raw("date(shipping_table.sold_date"),[$date1,$date2])
        ->groupBy('products.product_name')
        ->get();

请查看whereBetween因为我怀疑是否是语法错误的人。

错误:

  

语法错误或访问冲突:1064

1 个答案:

答案 0 :(得分:0)

知道了!你错过了a)你的条款:

$date1 = date("Y-m-d", strtotime($request->datepicker));
    $date2 = date("Y-m-d", strtotime($request->datepicker1));

    $products =  DB::table('shipping_table')
    ->select('products.product_name', 'products.price', DB::raw('Sum(shipping_products.quantity) AS qtysold'), 'shipping_table.sold_date')
    ->join('shipping_products','shipping_table.shipping_id', '=', 'shipping_products.shipping_id')
    ->join('products','products.product_id', '=', 'shipping_products.product_id')
    ->where('shipping_table.shipping_status',1)
    ->whereBetween(DB::raw("date(shipping_table.sold_date)"),[$date1,$date2])
    ->groupBy('products.product_name')
    ->get();

立即尝试