我是laravel和mysql的新手
mysql查询是
select * from table where id in ( select max(id) from table group by thread_id)
我的桌子是
id thread_id
1 45a7
2 123c
3 45a7
4 d056
5 123c
mysql表的输出是
id thread_id
3 45a7
5 123c
4 d056
这里我试图将我的mysql查询转移到laravel,如下所示
MODEL::whereIn('id', function($query){ $query->groupBy('thread_id')})->get();
我做错了什么?
答案 0 :(得分:1)
以下内容应符合您的查询
Model::whereIn('id', function($query) { $query->selectRaw('max(id)')->from('table')->groupBy('thread_id'); })->toSql();
这将输出
select * from "table" where "id" in (select max(id) from table group by "thread_id")