将SQL内部联接自定义转换为Eloquent Laravel

时间:2018-05-17 06:28:33

标签: mysql laravel eloquent laravel-5.4

我可以将自定义SQL语法转换为Eloquent Laravel,这是我的SQL语法:

SELECT a.* FROM krs a INNER JOIN (
                SELECT kode_matakuliah, MAX(bobot) as bobot_max
                FROM krs
                WHERE nim = 133341043
                GROUP BY kode_matakuliah
            ) b ON a.bobot = b.bobot_max AND a.kode_matakuliah = b.kode_matakuliah 
WHERE nim = 133341043

1 个答案:

答案 0 :(得分:0)

得了!我找到了方法。

Krs::select(DB::raw('krs.*'))
   ->join(DB::raw('(SELECT kode_matakuliah, MAX(bobot) as bobot_max
       FROM krs
       WHERE nim = 133341043
       GROUP BY kode_matakuliah) as b'),function($join){
       $join->on('krs.kode_matakuliah', '=', 'b.kode_matakuliah')
          ->on('krs.bobot', '=', 'b.bobot_max');
       })
   ->where('krs.no_bukti',1)
   ->where('krs.nim',133341043)->get();