Laravel 雄辩的获取语法错误或访问冲突错误
在Laravel 雄辩的获取语法错误或访问冲突错误,laravel,laravel-5,eloquent,Laravel,Laravel 5,Eloquent,在laravel 5中,当尝试执行此sql命令时,我在phpmyadmin上没有得到任何错误: 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' 在laravel上的此查询为: $userShoppin
laravel 5
中,当尝试执行此sql命令时,我在phpmyadmin
上没有得到任何错误:
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'
在laravel上的此查询为:
$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
尝试以下查询,在这里不使用
”
引号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');
我认为你的问题是正确的 您可以尝试使用以下查询代码
$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上可能有错误
以下内容是否适用于您?我是说添加groupBy?@Maraboc不,它不适用于->groupBy('shoppings.id')
@Mahdi.Pishguy:试试我的最新答案什么错误?共享错误?我将->ip()
更新为->ip
这个错误:语法错误或访问冲突:1140组的混合
@Mahdi.Pishguy:use$sum_of_ordering_count=$userShoppings->sum('ordering_count');