在laravel查询构建器中使用OR

时间:2017-07-22 09:20:41

标签: mysql laravel query-builder laravel-query-builder

我的表定义如下

**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

  1. payment_status = 1count_vb = 0
  2. payment_status = 0remain_partial_amount > 0
  3. 如果以上任何条件成立,则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');
    

1 个答案:

答案 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');