2017-12-27 202 views
2

laravel 5時嘗試執行SQL命令其我不明白phpmyadmin任何錯誤:在laravel雄辯越來越語法錯誤或訪問衝突錯誤

SELECT shoppings.*, sum(shoppings.ordering_count) 
FROM `shoppings` join products on products.id = shoppings.product_id 
where `shoppings`.`user_ordering_ip` = '127.0.0.1' 

與此查詢爲:

$userShoppings = \DB::table('shoppings') 
    ->join('products', 'shoppings.product_id', '=', 'products.id') 
    ->select('*', \DB::raw('sum(shoppings.ordering_count)')) 
    ->where('shoppings.user_ordering_ip', "'".request()->ip()."'") 
    ->get(); 

我得到這個錯誤:

SQLSTATE[42000]: Syntax error or access violation: 1140 Mixing of 
GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is 
illegal if there is no GROUP BY clause 
+0

的可能的複製[MySQL的#1140 - 組列混合](https://stackoverflow.com/questions/1244169/mysql-1140-混合組列) – Maraboc

+0

下面是爲你工作嗎?我的意思是添加groupBy? – Maraboc

+0

@Maraboc不,它不適合我 –

回答

1

與下面的查詢嘗試,沒有在這裏request()->ip'報價,我懷疑你是不是在這裏呼籲ip()方法,也不使用sum方法這裏

$userShoppings = \DB::table('shoppings as s') 
    ->join('products as p', 's.product_id', '=', 'p.id') 
    ->select('p.*','s.*') 
    ->where('s.user_ordering_ip', request()->ip()) 
    ->get(); 

$sum_of_ordering_count = $userShoppings->sum('ordering_count'); 
+0

我也收到錯誤 –

+0

什麼錯誤?分享錯誤?我更新了' - > ip()'到' - > ip' – OIIO

+0

這個錯誤:'Syntax error or access violation:1140 Mixing of GROUP' –

0

我覺得您的查詢是正確的。

您可以嘗試使用以下查詢代碼。

$userShoppings = \DB::table('shoppings') 
    ->join('products', 'shoppings.product_id', '=', 'products.id') 
    ->select('shoppings.*', \DB::raw('sum(shoppings.ordering_count)')) 
    ->where('shoppings.user_ordering_ip', '=', '127.0.0.1') 
    ->get(); 

而且似乎有可能是錯誤的request()->ip

相關問題