Laravel中的子查询

时间:2017-01-18 12:52:04

标签: laravel laravel-5 laravel-5.2

我在MySQL中有一个查询

SELECT * FROM group_recordings WHERE id IN ( SELECT MAX(id) FROM group_recordings GROUP BY time_span )

在“IN”运算符中有一个子查询,我想在laravel eloquent中转换此查询,我有GroupRecording Model。任何人帮助我

1 个答案:

答案 0 :(得分:0)

我认为这种查询的Laravel风格将是,

GroupRecording::whereIn('id', function($query) {
                $query->selectRaw('MAX(id)')
                ->from('group_recordings')
                ->groupBy('time_span');
            })
            ->where('time_span', '>', $recording->time_span)
            ->get();