2016-06-18 92 views
0

我有這樣的查詢:使用Laravel 5中的查詢生成器進行復雜查詢(使用三級子查詢)?

SELECT p.*, t3.gambar_meteran, t3.angka_meteran 
    from pelanggan p 
LEFT JOIN (SELECT t1.* 
    FROM tagihan t1 
     JOIN (SELECT no_rek_pelanggan, MAX(created_at) created_at 
      FROM tagihan 
      GROUP BY no_rek_pelanggan) t2 
     ON t1.no_rek_pelanggan = t2.no_rek_pelanggan 
     AND t1.created_at = t2.created_at) t3 
     ON p.no_rek = t3.no_rek_pelanggan` 

我如何使用,在laravel 5.2複雜的查詢?

我嘗試過很多辦法像使用DB::rawDB:statement

+0

請分享自己的所有嘗試究竟什麼錯誤信息或者你遇到的意外行爲! – Shadow

回答

0

我解決了這個問題,它:

$temptable=DB::raw("(SELECT t1.* 
      FROM tagihan t1 
      JOIN (SELECT no_rek_pelanggan, MAX(created_at) created_at 
       FROM tagihan 
       GROUP BY no_rek_pelanggan) t2 
      ON t1.no_rek_pelanggan = t2.no_rek_pelanggan 
      AND t1.created_at = t2.created_at) as t3"); 

     $query = DB::table('pelanggan as p') 
     ->select('p.*','t3.gambar_meteran') 
     ->leftJoin($temptable,'p.no_rek','=','t3.no_rek_pelanggan') 
     ->get(); 

`