2017-12-27 382 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