Say for example two users talk via direct message, you will have a conversation of messages .. I want to just select the latest message from that conversation and then show it as the conversation link in their message inbox … just how Facebook and Twitter messages work. They can then click that last sent message to view the whole conversation.
My messages table that contains all messages sent between users is in the following format:
sourceUserId is the id of the user who sent the message, targetUserId is the id of the user who received the message, body is the message, time is a timestamp of when the message was sent. I have kept body as abc… and time 1234 to keep this example simple, they are all different values.
+----+--------------+--------------+--------+------+
| id | sourceUserId | targetUserId | body | time |
+----+--------------+--------------+--------+------+
| 1 | 1 | 2 | abc... | 1234 |
| 2 | 3 | 1 | abc... | 1234 |
| 3 | 3 | 1 | abc... | 1234 |
| 4 | 1 | 3 | abc... | 1234 |
| 5 | 2 | 1 | abc... | 1234 |
| 6 | 1 | 2 | abc... | 1234 |
| 7 | 3 | 1 | abc... | 1234 |
| 8 | 4 | 1 | abc... | 1234 |
| 9 | 5 | 4 | abc... | 1234 |
| 10 | 3 | 2 | abc... | 1234 |
+----+--------------+--------------+--------+------+
To get all the messages (sent and received) for one user, I use this query:
SELECT sourceUserId, targetUserId, body, UNIX_TIMESTAMP(time)
FROM `usermessages`
WHERE targetUserId = 1
OR sourceUserId = 1
ORDER BY id DESC
LIMIT 10
+----+--------------+--------------+--------+------+
| id | sourceUserId | targetUserId | body | time |
+----+--------------+--------------+--------+------+
| 1 | 1 | 2 | abc... | 1234 |
| 2 | 3 | 1 | abc... | 1234 |
| 3 | 3 | 1 | abc... | 1234 |
| 4 | 1 | 3 | abc... | 1234 |
| 5 | 2 | 1 | abc... | 1234 |
| 6 | 1 | 2 | abc... | 1234 |
| 7 | 3 | 1 | abc... | 1234 |
| 8 | 4 | 1 | abc... | 1234 |
+----+--------------+--------------+--------+------+
but it returns multiple instances of the same message conversation and not just the latest message from a conversation between two users. Take for example rows 2,3 and 4 would all show exactly the same conversation.
I can get the query to work for just targetUserId (messages the user has received) with the following query:
SELECT sourceUserId, targetUserId, body, UNIX_TIMESTAMP(time)
FROM `usermessages`
WHERE targetUserId = 1
GROUP BY sourceUserId
ORDER BY id DESC
LIMIT 10
+----+--------------+--------------+--------+------+
| id | sourceUserId | targetUserId | body | time |
+----+--------------+--------------+--------+------+
| 2 | 3 | 1 | abc... | 1234 |
| 5 | 2 | 1 | abc... | 1234 |
| 8 | 4 | 1 | abc... | 1234 |
+----+--------------+--------------+--------+------+
and the opposite (messages sent by the user) with this, note that WHERE and GROUP BY have just been swapped:
SELECT sourceUserId, targetUserId, body, UNIX_TIMESTAMP(time)
FROM `usermessages`
WHERE sourceUserId = 1
GROUP BY targetUserId
ORDER BY id DESC
LIMIT 10
+----+--------------+--------------+--------+------+
| id | sourceUserId | targetUserId | body | time |
+----+--------------+--------------+--------+------+
| 1 | 1 | 2 | abc... | 1234 |
| 4 | 1 | 3 | abc... | 1234 |
+----+--------------+--------------+--------+------+
but if I combine the two results and group by targetUserId, sourceUserId then it does not give the correct result because all outgoing messages from 1 to (2,3,4) are grouped.
What I would like to return
I think the Pseudo Code for such a query would be:
SELECT sourceUserId, targetUserId, body, UNIX_TIMESTAMP(time)
FROM `usermessages`
WHERE sourceUserId = 1
OR targetUserId = 1
GROUP BY (If targetUserId != 1), (If sourceUserId != 1)
ORDER BY id DESC
LIMIT 10
+----+--------------+--------------+--------+------+
| id | sourceUserId | targetUserId | body | time |
+----+--------------+--------------+--------+------+
| 1 | 1 | 2 | abc... | 1234 |
| 2 | 3 | 1 | abc... | 1234 |
| 8 | 4 | 1 | abc... | 1234 |
+----+--------------+--------------+--------+------+
VERSION 2
This is untested; it is closer but still not right… out of time though on working on this…
VERSION 1 (Miss)
IMO you need to group by body as well.
Or use distinct
I think the problem is really in that body is different or timestamp is different for those records; if not, then why are they duplicated in the table? Specifically in your output why are records 1 & 6 there… are you missing a unique index/pk which would prevent duplicates?