2017-06-20 49 views
-1

我正在嘗試在laravel中創建此查詢。這個查詢有助於從不同的表中獲得患者的總賬單和該患者已支付的總額 患者表:其中患者記錄被提取 賬單表:其中存儲患者賬單 支付表:其中所有支付由病人店在Laravel中創建多個JOIN

SELECT A.id,A.name,SUM(B.totalInvoice) as totalAmount,SUM(C.totalPayed) 
as totalPayed 
FROM  patients as A 
LEFT JOIN 
(
select patient_id 
    ,  sum(amount) as totalInvoice 
    from billings 
    group by 
      patient_id 
    ) as B 
ON  B.patient_id = A.id 
LEFT JOIN 
    (
    select patient_id 
    ,  sum(amount) as totalPayed 
    from payments 
    group by 
      patient_id 
    ) as C 
ON  C.patient_id = A.id 

這怎麼能在laravel創建

回答

0

試試這個代碼來創建laravel多個連接

DB::table('patients')->select(
          'patients.id', 
          'patients.name as name', 
          \DB::raw('SUM(billings.amount) as totalAmount'), 
          \DB::raw('SUM(payments.amount) as totalPayed') 
          ) 
          ->join('billings','billings.patient_id','=','patients.id')->groupBy('billings.patient_id') 
          ->join('payments','payments.patient_id','=','patients.id')->groupBy('payments.patient_id') 
          ->get() 
+0

仍然不給出與查詢相同的結果 – devloper2009