我有这样的查询:
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::raw
,DB:statement
等等。
答案 0 :(得分: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();
`