I am trying to get a users most current messages for each conversation. However my group by query doesnt seem to be bringing the correct rows back. Here is my data:
ID | MESSAGE | RELATED_ID | DATE_SENT
2 | Hi | 2 | 2013-02-21 16:03:00
3 | Hii | 2 | 2013-02-21 16:04:00
4 | Hiii | 2 | 2013-02-21 16:05:00
5 | Hiiii | 2 | 2013-02-21 16:06:00
6 | Bye | 6 | 2013-02-21 16:03:01
7 | Byee | 6 | 2013-02-21 16:04:01
8 | Byeee | 6 | 2013-02-21 16:05:01
9 | Again | 9 | 2013-02-21 16:03:02
10| Againn | 9 | 2013-02-21 16:04:02
The result i am looking for is:
ID | MESSAGE | RELATED_ID | DATE_SENT
5 | Hiiii | 2 | 2013-02-21 16:06:00
8 | Byeee | 6 | 2013-02-21 16:05:01
10| Againn | 9 | 2013-02-21 16:04:02
My current query is:
SELECT MAX(ID), Message, Date_Sent, related_id FROM MESSAGES GROUP BY RELATED_ID LIMIT 0,100
The result that I get is:
ID | MESSAGE | RELATED_ID | DATE_SENT
5 | Hi | 2 | 2013-02-21 16:03:00
8 | Bye | 6 | 2013-02-21 16:03:01
10| Again | 9 | 2013-02-21 16:03:02
It seems to be getting the correct ID but not the correct data for that ID.
I would appreciate any help I can get on this.