【问题标题】:laravel between two dates query error Syntax error两个日期之间的laravel查询错误语法错误
【发布时间】:2018-03-21 15:58:16
【问题描述】:

请在我的查询中帮助我。我目前收到查询错误。我是 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

【问题讨论】:

  • 我认为您的问题出在date(shipping_table.sold_date in whereBetween!格式和$date1 = date("Y-m-d", strtotime($request->datepicker));一样,先看看这个!
  • 你能把$date1 = date("Y-m-d", strtotime($request->datepicker));的结果打印出来,你得到的究竟是什么格式?
  • 2017-10-09 还是string(10) "2017-10-09" ???
  • @HirenGohel 其字符串(10)“2017-10-09”
  • date(shipping_table.sold_date)是什么格式,和$date1格式一样吗?

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


【解决方案1】:

知道了!您在 between 子句上缺少 ):

$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();

立即尝试

【讨论】:

  • SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;
  • 它的格式已经很好了,先生,这里是 var_dump($date1);字符串(10)“2017-10-09”
  • 你使用的是严格模式吗?您可以在您的数据库连接配置数组(database.php 配置文件)中检查它
  • 您好先生,谢谢您的快速回复。我已经把严格模式设为假了
  • 仍然 500 内部服务器错误
猜你喜欢
  • 2013-02-11
  • 1970-01-01
  • 1970-01-01
  • 2014-03-16
  • 2015-06-17
  • 2022-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多