我有两个 mysql 表用户和消息。请您帮我创建一个 sql 查询,该查询仅返回由登录用户 Mike ( user_id=1 ) 发送或接收的最新消息以及消息被发送到的人的 message_id、message_text 和 first_name 或从......收到。按 message_id 降序排列。
到目前为止,我有一个查询: -
SELECT message_id, first_name, message_text FROM tbl_users, tbl_messages
WHERE
(tbl_users.user_id = tbl_messages.sender_id AND tbl_messages.receiver_id = 1
OR tbl_users.user_id = tbl_messages.receiver_id AND tbl_messages.sender_id = 1)
GROUP BY tbl_users.first_name
ORDER BY message_id DESC
这给出了下图中的结果。似乎 ORDER BY 被忽略了
先感谢您
tbl_users
---------------------------
| user_id | first_name |
---------------------------
| 1 | Mike |
| 2 | John |
| 3 | George |
| 4 | Peter |
| 5 | Sarah |
---------------------------
tbl_messages
----------------------------------------------------------------
| message_id | sender_id | receiver_id | message_text |
----------------------------------------------------------------
| 1 | 2 | 1 | Hello |
| 2 | 3 | 1 | How are you |
| 3 | 1 | 5 | Hi there |
| 4 | 2 | 1 | Greetings |
| 5 | 1 | 4 | Good day |
| 6 | 3 | 1 | Hi |
| 7 | 5 | 1 | A message |
| 8 | 5 | 4 | Good morning |
| 9 | 1 | 5 | Hello dear |
| 10 | 1 | 3 | Howdy |
----------------------------------------------------------------
期望的结果
----------------------------------------------
| message_id | first_name | message_text |
----------------------------------------------
| 10 | George | Howdy |
| 9 | Sarah | Hello dear |
| 5 | Peter | Good day |
| 4 | John | Greetings |
----------------------------------------------
数据库在这里可用