【发布时间】:2021-11-11 15:37:47
【问题描述】:
我有一张包含不同类型数据的表格。我要找
sum(typeA) - sum(typeB)
类型 A = whereIn('type', ['CreditNote','ReceiptVoucher'])
类型 B = ->whereIn('type', ['DebitNote','PaymentVoucher'])
这是我想出的查询,但它不起作用。我错过了什么?
Transaction::where('contact_id',$this->contact->id)->where('date', '<', $from)
->where(function ($query) {
$query
->whereIn('type', ['CreditNote','ReceiptVoucher'])
->selectRaw("SUM(amount) as in");
})
->Where(function ($query) {
$query
->whereIn('type', ['DebitNote','PaymentVoucher'])
->selectRaw("SUM(amount) as out");
})
->selectRaw("COALESCE(SUM(in),0) - COALESCE(SUM(out),0) as amount")
->groupBy('contact_id')
->get();
如果我找不到这样的差异,我也可以只得到两个和,然后我可以将它们减去。
【问题讨论】: