2017-07-22 41 views
1

我試圖運行以下Query with Query builder。Connection.php中的Laravel QueryException第647​​行:SQLSTATE [42000]:語法錯誤或訪問衝突

$products = \DB::table('users') 
      ->join('products','products.auth_id','users.id') 
      ->leftjoin('buys','products.id','buys.product_id') 
      ->select('products.*','users.avatar',DB::raw('COUNT(buys.product_id) as total_sells')) 
      ->groupBy('products.id')    
      ->where('products.status','=','1') 
      ->take(20) 
      ->paginate(4); 

Where in products.id could be there in buys table or not.. 

但我得到以下錯誤。 我改變了database.php mysql array from strict = true to false

QueryException in Connection.php line 647: 
SQLSTATE[42000]: Syntax error or access violation: 1055 'cart.products.name' isn't in GROUP BY 
+0

試着把你的'''groupBy''放在查詢的最後。 **編輯**:在'''where'之後''' – Treast

+0

我試過了同樣的錯誤我得到的錯誤 – User57

+0

噢,你的'''groupBy''需要指定你選擇的每一列(沒有聚合) 。所以基本上來自''''''''''和'''users.avatar'''的每一列。 – Treast

回答

0

嘗試

$products = \DB::table('users') 
->selectRaw('products.*,users.avatar, COUNT(buys.product_id) as total_sells') 
->where('products.status','=','1') 
     ->join('products','products.auth_id','=', 'users.id') 
     ->leftjoin('buys','products.id','buys.product_id') 
     ->groupBy('products.id')      
     ->take(20) 
     ->get(); 

我不知道如果PAGINATE將工作將查詢生成器。

+0

nope同樣的問題我正面臨 – User57

+0

哦忘記修改 - > leftjoin('buys','products.id','=','buys.product_id') – erashdan

+0

我試過了,也..雖然我didn' t添加購物車表,但沒有得到確切的輸出..它顯示'SQLSTATE [42000]:語法錯誤或訪問衝突:1055'cart.products.name'不在GROUP BY中 – User57

相關問題