【问题标题】:How to find the sum of multiple columns sum in single query如何在单个查询中找到多列总和的总和
【发布时间】:2020-10-09 19:56:53
【问题描述】:

在这里,我想获得(SaleQnt 的总和 + ReturnQnt 的总和 + WasteQnt 的总和)的总和。从这个查询中,我可以找到每个产品的所有总和。但无法获得所有个人的总和总和。

$return_products = DB::table('ready_products_allocate_return')
                    ->join('ready_product_allocate_return_details','ready_product_allocate_return_details.RtnID','ready_products_allocate_return.id')
                    ->select('ready_product_allocate_return_details.CatID','ready_product_allocate_return_details.ItemID','ready_product_allocate_return_details.ReturnQnt')
                    ->select('ready_product_allocate_return_details.CatID','ready_product_allocate_return_details.ItemID',DB::raw('sum(ready_product_allocate_return_details.SaleQnt) as tSaleQnt'),DB::raw('sum(ready_product_allocate_return_details.ReturnQnt) as tReturnQnt'),DB::raw('sum(ready_product_allocate_return_details.WasteQnt) as tWasteQnt'))
                    ->where('ready_product_allocate_return_details.StoreID',$wrhouseID)
                    ->where('ready_product_allocate_return_details.Date',$selDate)
                    ->groupBy('ready_product_allocate_return_details.ItemID')
                    ->get();

【问题讨论】:

    标签: laravel


    【解决方案1】:

    你可以像这样做出一个选择语句

    $return_products = DB::table('ready_products_allocate_return')
        ->selectRaw(
            'ready_product_allocate_return_details.CatID
            ,ready_product_allocate_return_details.ItemID
            ,ready_product_allocate_return_details.ReturnQnt
            ,ready_product_allocate_return_details.CatID
            ,ready_product_allocate_return_details.ItemID
            ,sum(ready_product_allocate_return_details.SaleQnt) as tSaleQnt
            ,sum(ready_product_allocate_return_details.ReturnQnt) as tReturnQnt
            ,sum(ready_product_allocate_return_details.WasteQnt) as tWasteQnt
            ,(tSaleQnt + tReturnQnt + tWasteQnt) as total'
        )
        ->join('ready_product_allocate_return_details', 'ready_product_allocate_return_details.RtnID', 'ready_products_allocate_return.id')
        ->where([
            'ready_product_allocate_return_details.StoreID' => $wrhouseID,
            'ready_product_allocate_return_details.Date' => $selDate
        ])
        ->groupBy('ready_product_allocate_return_details.ItemID')
        ->get();
    

    参考链接https://laravel.com/docs/8.x/queries#raw-methods


    或者如果你只想要total的总和

    $return_products = DB::table('ready_products_allocate_return')
        ->selectRaw(
            'ready_product_allocate_return_details.CatID
            ,ready_product_allocate_return_details.ItemID
            ,ready_product_allocate_return_details.ReturnQnt
            ,ready_product_allocate_return_details.CatID
            ,ready_product_allocate_return_details.ItemID
            ,(sum(ready_product_allocate_return_details.SaleQnt) + sum(ready_product_allocate_return_details.ReturnQnt) + sum(ready_product_allocate_return_details.WasteQnt)) as total'
        )
        ->join('ready_product_allocate_return_details', 'ready_product_allocate_return_details.RtnID', 'ready_products_allocate_return.id')
        ->where([
            'ready_product_allocate_return_details.StoreID' => $wrhouseID,
            'ready_product_allocate_return_details.Date' => $selDate
        ])
        ->groupBy('ready_product_allocate_return_details.ItemID')
        ->get();
        }
    

    【讨论】:

    • "message": "类型错误:传递给 Illuminate\\Database\\Query\\Builder::selectRaw() 的参数必须是数组类型,给定字符串。兄弟在这里得到这个错误在 '(tSaleQnt + tReturnQnt + tWasteQnt) 作为总计' 行
    • @Ohidul opss 立即尝试
    • ->where([ 'ready_product_allocate_return_details.StoreID' => $wrhouseID, 'ready_product_allocate_return_details.Date','like',$getMonth.'%' ]).这里如何使用like条件?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多