- 的表
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
,但我顯然得到一組用戶和另一組的消息的接收的消息的用戶發送。
獲取我:
+-------------------------------+--------------+------------+---------------------+
| 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,然後將它們分組 –
已經嘗試過,我編輯的嘗試的問題。分組工作正確,但我沒有收到最後一條消息 – StoryTeller
爲什麼在最終結果中沒有「14,15」消息? –