【问题标题】:Laravel 5.3 “group by count” query issuesLaravel 5.3 “按计数分组”查询问题
【发布时间】:2016-10-19 09:48:57
【问题描述】:

请我尝试在原始 sql 中运行类似这样的查询

SELECT `qnty`, COUNT(*) FROM cartlist GROUP BY `pro_id`,`cart_id`,`price`

在 laravel 中。

我试过了

$count = DB::table('cartlist')
                 ->select(DB::raw('count(*) as qnty'))
                 ->where('pro_id', '$tt->pro_id')
                 ->where('cart_id', '$cart_id')
                 ->groupBy('pro_id','price')
                 ->get();

但它给出了以下错误

Illuminate\Support\Collection 类的对象无法转换为 int

【问题讨论】:

  • 为什么$cart_id$tt->pro_id周围有单引号?
  • $count = DB::table('cartlist') ->select(DB::raw('count(*) as qnty')) ->where('pro_id', $tt- >pro_id) ->where('cart_id', $cart_id) ->groupBy('pro_id','price') ->get();
  • 但仍然出现该错误
  • 您的原始 sql 查询和 laravel 查询确实不同。根据您的原始 SQL 查询,您按pro_idcart_idprice 分组,根据您的 laravel 查询,您仅按“pro_id”分组,并使用其他三个作为 where 条件。你想达到什么目的?

标签: laravel laravel-5 laravel-5.2 laravel-5.1


【解决方案1】:
$count = DB::table('cartlist') ->select(DB::raw('count(*) as qnty')) 
                               ->where('pro_id', '$tt->pro_id') 
                               ->where('cart_id', '$cart_id') 
                               ->groupBy('pro_id','price','cart_id') ->get();

【讨论】:

    猜你喜欢
    • 2017-04-22
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 2020-12-05
    • 2015-12-19
    • 1970-01-01
    • 2017-06-21
    相关资源
    最近更新 更多