gpt4 book ai didi

mysql - SQL:如何按两列的唯一组合进行分组?

转载 作者:行者123 更新时间:2023-11-29 05:07:02 24 4
gpt4 key购买 nike

上下文:

  • message 包含列 from_user_idto_user_id
  • 用户应该看到最近的对话以及显示的最后一条消息
  • 一个 session 由多条消息组成,这些消息具有相同的用户 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_idto_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 |
+-------------------------------+--------------+------------+---------------------+

我怎样才能做到这一点?

编辑:使用 leastgreatest 也不会导致所需的结果。它确实对条目进行了正确分组,但正如您在结果中看到的那样,最后一条消息不正确。

+----+-------------------------------------------------+------+---------------------+--------------+------------+
| 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 |
+----+-------------------------------------------------+------+---------------------+--------------+------------+

最佳答案

做你想做的事情的一种方法是使用相关子查询,找到匹配对话的最小创建日期/时间:

SELECT m.*
FROM message m
WHERE 13 in (from_user_id, to_user_id) AND
m.created = (SELECT MAX(m2.created)
FROM message m2
WHERE (m2.from_user_id = m.from_user_id AND m2.to_user_id = m.to_user_id) OR
(m2.from_user_id = m.to_user_id AND m2.to_user_id = m.from_user_id)
)
ORDER BY m.created DESC

关于mysql - SQL:如何按两列的唯一组合进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46007597/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com