lundi 25 mai 2015

select specific number of results for a column value from DB

I have a table of "messages". In this table I have these columns: messages(id, from, to, body, new, received_at)

I want to fetch a collection of all messages but maximum 2 new messages for each of "from";

lets say I have total 50 messages from 3 different unique users. but I want to fetch maximum 2 messages from each user.

I tried to filter results after fetching all messages but I think that is waste of resources. How to do it efficiently with single query?

Aucun commentaire:

Enregistrer un commentaire