【发布时间】:2017-09-01 20:29:05
【问题描述】:
上下文:
- 表
message具有from_user_id和to_user_id列 - 用户应该会看到最近的对话并显示最后一条消息
- 一个会话由多条消息组成,这些消息具有相同的用户 ID 组合(用户发送消息,用户接收消息)
表格内容:
+-------------------------------------------------+--------------+------------+
| text | from_user_id | to_user_id |
+-------------------------------------------------+--------------+------------+
| Hi there! | 13 | 14 | <- Liara to Penelope
| Oh hi, how are you? | 14 | 13 | <- Penelope to Liara
| Fine, thanks for asking. How are you? | 13 | 14 | <- Liara to Penelope
| Could not be better! How are things over there? | 14 | 13 | <- Penelope to Liara
| Hi, I just spoke to Penelope! | 13 | 15 | <- Liara to Zara
| Oh you did? How is she? | 15 | 13 | <- Zara to Liara
| Liara told me you guys texted, how are things? | 15 | 14 | <- Zara to Penelope
| Fine, she's good, too | 14 | 15 | <- Penelope to Zara
+-------------------------------------------------+--------------+------------+
我的尝试是按from_user_id 和to_user_id 进行分组,但我显然得到了用户收到的一组消息和用户发送的另一组消息。
SELECT text, from_user_id, to_user_id,created FROM message
WHERE from_user_id=13 or to_user_id=13
GROUP BY from_user_id, to_user_id
ORDER BY created DESC
得到我:
+-------------------------------+--------------+------------+---------------------+
| text | from_user_id | to_user_id | created |
+-------------------------------+--------------+------------+---------------------+
| Oh you did? How is she? | 15 | 13 | 2017-09-01 21:45:14 | <- received by Liara
| Hi, I just spoke to Penelope! | 13 | 15 | 2017-09-01 21:44:51 | <- send by Liara
| Oh hi, how are you? | 14 | 13 | 2017-09-01 17:06:53 |
| Hi there! | 13 | 14 | 2017-09-01 17:06:29 |
+-------------------------------+--------------+------------+---------------------+
虽然我想要:
+-------------------------------+--------------+------------+---------------------+
| text | from_user_id | to_user_id | created |
+-------------------------------+--------------+------------+---------------------+
| Oh you did? How is she? | 15 | 13 | 2017-09-01 21:45:14 | <- Last message of conversation with Zara
| Oh hi, how are you? | 14 | 13 | 2017-09-01 17:06:53 |
+-------------------------------+--------------+------------+---------------------+
我怎样才能做到这一点?
编辑:
使用least 或greatest 也不会产生所需的结果。
它确实对条目进行了正确分组,但正如您在结果中看到的那样,最后一条消息不正确。
+----+-------------------------------------------------+------+---------------------+--------------+------------+
| id | text | read | created | from_user_id | to_user_id |
+----+-------------------------------------------------+------+---------------------+--------------+------------+
| 8 | Oh you did? How is she? | No | 2017-09-01 21:45:14 | 15 | 13 |
| 5 | Could not be better! How are things over there? | No | 2017-09-01 17:07:47 | 14 | 13 |
+----+-------------------------------------------------+------+---------------------+--------------+------------+
【问题讨论】:
-
我们能不能对 to_user_id 应用最小,对 from_user_id 应用最大,然后对它们进行分组
-
已经尝试过了,我编辑了对该问题的尝试。分组工作正常,但我没有收到最后一条消息
-
为什么最终结果中没有
14, 15消息? -
那些是故意错误的列,我只想要一个用户的消息:
WHERE from_user_id=13 or to_user_id=13 -
那么对于用户 13,您希望所有对话伙伴(14 和 15)都拥有最新的帖子吗?不管#13 是发送者还是接收者?我明白为什么是“哦,你做到了?她怎么样?”对于#15。但为什么不是“再好不过了!那边的情况怎么样?” #14?