将MySQL查询转换为Laravel查询

时间:2018-02-16 14:22:51

标签: mysql laravel laravel-5

我想将mysql查询转换为laravel查询。

MySQL的:

SELECT * FROM scadenze WHERE created_at IN(SELECT MAX(created_at) FROM scadenze GROUP BY processo_id)

Laravel(我收到错误,类stdClass的对象无法转换为字符串):

DB::table('scadenze')->whereIn('created_at', DB::select("( SELECT MAX(created_at) FROM scadenze GROUP BY processo_id )"))->get();

2 个答案:

答案 0 :(得分:2)

DB::table('scadenze')->whereIn('created_at', function($query) {
   $query->from('scadenze')
         ->groupBy('processo_id')
         ->select(DB::raw('max(created_at)');
})->get();

答案 1 :(得分:0)

试试这个:

DB::table('scadenze')
    ->whereRaw(DB::raw("created_at IN (SELECT MAX(created_at) FROM scadenze GROUP BY processo_id)"))
    ->get();

我希望这可以提供帮助!