例如,两个用户通过直接消息交谈,您将进行消息对话..我只想从该对话中选择最新消息,然后将其显示为他们的消息收件箱中的对话链接......就像 Facebook 和 Twitter消息工作。然后,他们可以单击最后发送的消息以查看整个对话。
我的消息表包含用户之间发送的所有消息,格式如下:
sourceUserId 是发送消息的用户 id,targetUserId 是接收消息的用户 id,body 是消息,time 是消息发送时间的时间戳。我将 body 保留为 abc... 和 time 1234 以使此示例保持简单,它们都是不同的值。
+----+--------------+--------------+--------+------+
| 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 |
+----+--------------+--------------+--------+------+
要获取一个用户的所有消息(发送和接收),我使用以下查询:
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 |
+----+--------------+--------------+--------+------+
但它返回同一消息对话的多个实例,而不仅仅是来自两个用户之间的对话的最新消息。以第 2,3 和 4 行为例,它们都将显示完全相同的对话。
我可以使用以下查询使查询仅适用于 targetUserId(用户收到的消息):
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 |
+----+--------------+--------------+--------+------+
与此相反(用户发送的消息),请注意 WHERE 和 GROUP BY 刚刚交换:
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 |
+----+--------------+--------------+--------+------+
但是如果我将这两个结果结合起来,group by targetUserId, sourceUserId
然后它不会给出正确的结果,因为从 1 到 (2,3,4) 的所有传出消息都被分组了。
我想退货
我认为此类查询的伪代码是:
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 |
+----+--------------+--------------+--------+------+