I have 3 tables which are orders, commissions and order_items. Below is the table structures
Orders
Commissions
Order Items
I want to get Totla commissions, and Total amount using joins.
- Total amount is giving correct figure
- Total commssions is not
How I know this is by getting the data separately by using one join (which works correctly), but using 2 joins does not.
Getting total commissions only (Gives correct values)
$getstats = Order::select(
DB::raw('SUM(sc.amount) as totalcommissions')
)
->leftJoin('commissions as sc', 'sc.order_id', '=', 'orders.id')
->first();
Getting both total commissions and total amount (wrong values)
$getstats = Order::select(
DB::raw('SUM(sc.amount) as totalcommissions'),
DB::raw('SUM(oi.qty * oi.each) as totalamount')
)
->leftJoin('order_items as oi', 'oi.order_id', '=', 'orders.id')
->leftJoin('commissions as sc', 'sc.order_id', '=', 'orders.id')
->first();
Does not give correct values.
Where am I going wrong.


