I have a 'stream_update' table that stores the updates users post to the groups they're subscribed to. The table has the following fields:
- id (index, auto increment)
- user_id
- group_id
- stream_update
- timestamp
Basically I'm trying to write a query that gets the last stream update for every user in each group they're subscribed to. So if a user is subscribed to 3 groups, it would get the last update he has written in each of these groups.
I've tried the following:
SELECT * FROM stream_update GROUP BY group_id, user_id
This seemed to work, but it would get the oldest records not the newest. Adding "ORDER BY" to that doesn't work, since it orders the already-fetched records only.
Any idea how I can write a query for this?
Your help is much appreciated :)