简体   繁体   中英

Laravel sum join with condition

I have products, warehouses, transactions and transaction_details table.

$p = Product::leftJoin('transaction_details', 'products.id', '=' ,'transaction_details.product_id')
    ->leftJoin('transactions', 'transaction_details.transaction_id', '=', 'transactions.id')
    ->leftJoin('keep_details', 'products.id', '=', 'keep_details.product_id')
    ->selectRaw(
                    'products.id,products.product_name productName, products.category_id,
                    products.sub_category_id, IFNULL(sum(transaction_details.quantity), 0) as totalQty,
                    products.package_qty, IFNULL(sum(keep_details.quantity), 0) as totalKeep'
                )
    ->groupBy('id','productName','category_id', 'sub_category_id', 'package_qty')
    ->get();
    return new StockCollection($p);

By using the above query I am able to get all the products quantity as expected. But in some cases, I just want the stock quantity of a specific warehouse. I've tried this by added where clause of warehouse Id which is related to the transactions table.

$p = Product::leftJoin('transaction_details', 'products.id', '=' ,'transaction_details.product_id')
    ->leftJoin('transactions', 'transaction_details.transaction_id', '=', 'transactions.id')
    ->leftJoin('keep_details', 'products.id', '=', 'keep_details.product_id')
    ->selectRaw(
                    'products.id,products.product_name productName, products.category_id,
                    products.sub_category_id, IFNULL(sum(transaction_details.quantity), 0) as totalQty,
                    products.package_qty, IFNULL(sum(keep_details.quantity), 0) as totalKeep'
                )
    ->where('transactions.warehouse_id', 1) // add this line ####################
    ->groupBy('id','productName','category_id', 'sub_category_id', 'package_qty')
    ->get();
    return new StockCollection($p);

There aren't any errors but It not showing all the products as I expected, that's because some product does not contain any transaction in the warehouse yet, But I want to show all products even when it does not has any transactions yet just showing 0 quantity. I know that's not too complicated but I am just a noob in this, therefore How can I accomplish that? Thank you in advance...

Now it's worked as expected by using a subquery like this:

$p = Product::leftJoin(DB::raw('(select td.product_id, td.quantity from products as p left JOIN transaction_details as td on
        td.product_id = p.id left join transactions as t on t.id = td.transaction_id
        where t.warehouse_id = 1) as details'), 'products.id', '=' ,'details.product_id')
    ->leftJoin('keep_details', 'products.id', '=', 'keep_details.product_id')
    ->selectRaw('products.id,products.product_name as productName, products.category_id,
                    products.sub_category_id, IFNULL(sum(details.quantity), 0) as totalQty,
                    products.package_qty, IFNULL(sum(keep_details.quantity), 0) as totalKeep')
    ->groupBy('id','productName','category_id', 'sub_category_id', 'package_qty')
    ->get();
    return new StockCollection($p);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM