mysql - Get most recent row with group by and Laravel -
even though there multiple questions can't query return row recent date group by.
i have following table..
| message_id | | | created_at | status ---------------------------------------------- | 1 | 1 | 2 | 2017-04-06 | 1 | 2 | 1 | 2 | 2017-04-07 | 0 | 3 | 3 | 4 | 2017-04-06 | 1 | 4 | 3 | 4 | 2017-04-07 | 0 ----------------------------------------------
and i'm tryin rows recent date.
| message_id | | | created_at | status ---------------------------------------------- | 2 | 1 | 2 | 2017-04-07 | 0 | 4 | 3 | 4 | 2017-04-07 | 0
currently query returns rows last recent date.
$messages = message::where('to', auth::id()) ->groupby('from') ->orderby('created_at', 'desc') ->paginate(10);
the problem result set first grouped ordered. can use nested select want.
sql query:
select t.* (select * messages order created_at desc) t group t.from
with laravel:
$messages = message::select(db::raw('t.*')) ->from(db::raw('(select * messages order created_at desc) t')) ->groupby('t.from') ->get();
you need add where()
clauses.
Comments
Post a Comment