我的表定义如下
**PURCHASE_PYAMENTS**
SELLER MASTER_PAYMENT BILL COUNT_VB PAYMENT_STATUS REMAIN_PARTIAL_AMOUNT
abc 15000 0 0 1 0
xyz 14000 1 1 1 14000
abc 15000 0 0 0 5000
在两种情况下,我需要sum
master_payment
列的特定seller
:
payment_status = 1
和count_vb = 0
payment_status = 0
和remain_partial_amount > 0
如果以上任何条件成立,则master_payment
应添加sum
,那么,如何使用单个查询来实现?
我自己尝试但是只得到一个条件的结果我的查询如下:
$total_count_vb = DB::table('purchase_payments')
->where('seller',$seller_list)
->where('count_vb',0)
->where('payment_status',1)
->SUM('master_payment');
答案 0 :(得分:0)
对parameter grouping使用where()
和orWhere()
个闭包:
Payment::where('seller', 'abc')->
->where(function ($q) {
$q->where(function ($q) {
$q->where('payment_status', 1)->where('count_vb', 0);
})
->orWhere(function ($q) {
$q->where('payment_status', 0)->where('remain_partial_amount', '>', 0);
});
});
->sum('master_payment');