我正在努力获得所有工作账单的收据和扣除总额。收据中有2个条目,金额为Rs.80,000和Rs.10,000。 扣除中有2个条目,金额为Rs.500和Rs.100
我得到的结果是: 扣除总额 - 1,200卢比(实际Rs600的两倍) 总收入--1,80,000卢比(实际Rs.90,000的两倍)
我的Eloquent查询如下:
$bills = bills::leftjoin('receipts', 'receipts.billId', 'bills.billId')->leftjoin('deductions', 'deductions.billId', 'bills.billId')
->select('bills.billId', 'bills.billDate', 'bills.billNumber', 'bills.billStatus', 'bills.billAmount', DB::raw('SUM(deductions.deductionAmount) as totalDeductions'), DB::raw('SUM(receipts.receiptAmount) as totalReceipts'))
->groupBy('bills.billId', 'bills.billDate', 'bills.billNumber', 'bills.billStatus', 'bills.billAmount')
->where('bills.workId', $workId)
->get();
SQL小提琴链接 - http://www.sqlfiddle.com/#!9/cf46cb/1/0
答案 0 :(得分:1)
如果删除分组和SUM,则可以看到返回的所有行,并且左连接导致重复错误:
SELECT `bills`.`billId`, `bills`.`billDate`, `bills`.`billNumber`, `bills`.`billAmount`,
deductions.deductionAmount,
receipts.receiptAmount
FROM `bills` left join `receipts` on `receipts`.`billId` = `bills`.`billId`
left join `deductions` on `deductions`.`billId` = `bills`.`billId`
WHERE `bills`.`workId` = 6
你可以通过子查询避免这种情况,但我不知道如何用Eloquent做到这一点:
SELECT b.`billId`, b.`billDate`, b.`billNumber`, b.`billAmount`,
(SELECT SUM(deductionAmount) FROM deductions WHERE BillID = b.BillId) as totalDeductions,
(SELECT SUM(receiptAmount) FROM receipts WHERE BillID = b.BillId) as totalReceipts
FROM `bills` b
WHERE b.`workId` = 6
答案 1 :(得分:0)
好的,这就是在Eloquent中的表现:
renderer.setStyle(document.documentElement, `--${cssVariableName}`, `${value}`);