dtx9763 2014-07-25 15:51 采纳率: 100%
浏览 45
已采纳

返回表中每条记录的平均值

I have a table that returns categories for products. Their is another table (called inventory_review) that has reviews that customers have left for the products. How can I in the foreach loop of the actual category, also loop through each review to find the average rating for that particular product.

I can easily do it on the product page itself and just use AVG on the statement, but on the categories it's only showing the first review.

Mysql

     $category = Inventory::select('inventory_review.rating','inventory_images.image', 'inventory.id' , 'inventory.sku', 'inventory.name', 'inventory.price', 'inventory_categories.category')
        ->join('inventory_categories', 'inventory.sku', '=', 'inventory_categories.sku')
        ->leftJoin('inventory_images', 'inventory.sku', '=', 'inventory_images.sku')
        ->leftJoin('inventory_review', 'inventory.id', '=', 'inventory_review.inventory_id')
        ->where('inventory_categories.category', 'LIKE', '%'.$cat.'%')
        ->where('inventory.active', '=', 1)
        ->where('inventory.stock_quantity', '>', 2)
        ->groupby('inventory.id')
        ->paginate(16);

So basically what happens is it will loop through all the products that matches the category and only return the 1st review rating for the product. I know I probably need to loop through something but not sure what.

@if($inventory->rating > 0)
    @for ($i=1; $i <= 5 ; $i++)
        @if($i <= $inventory->rating)
            <i class="fa fa-star"></i>
        @else
            <i class="fa fa-star-o"></i>
        @endif
    @endfor
@endif
  • 写回答

2条回答 默认 最新

  • duanshan2988 2014-07-25 17:58
    关注

    You should use the AVG() function here as well. (Don't know the class you're using, but I would guess something like this)

    $category = Inventory::select('AVG(inventory_review.rating) AS averageRating','inventory_images.image', 'inventory.id' , 'inventory.sku', 'inventory.name', 'inventory.price', 'inventory_categories.category')
            ->join('inventory_categories', 'inventory.sku', '=', 'inventory_categories.sku')
            ->leftJoin('inventory_images', 'inventory.sku', '=', 'inventory_images.sku')
            ->leftJoin('inventory_review', 'inventory.id', '=', 'inventory_review.inventory_id')
            ->where('inventory_categories.category', 'LIKE', '%'.$cat.'%')
            ->where('inventory.active', '=', 1)
            ->where('inventory.stock_quantity', '>', 2)
            ->groupby('inventory.id')
            ->paginate(16);
    

    Since you're using GROUP BY at the end, only the first reviews rating will be returned if you don't specify what it should do with the rating of each review (AVG(), MAX(), MIN(), SUM() or what it may be you wish to accomplish).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上