我有两个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 |
----------------------------------------------
数据库可用here
答案 0 :(得分:3)
只需添加 GROUP BY
条款就可以解决您的问题,试试这个......
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.user_id
ORDER BY message_id DESC
更新:更改整个查询
SELECT t2.message_id, t2.first_name, t2.message_text FROM
(SELECT u.user_id, m.message_id, u.first_name, m.message_text FROM `tbl_users` u
JOIN `tbl_messages` m ON (u.user_id = m.sender_id AND m.receiver_id = 1)
OR (u.user_id = m.receiver_id AND m.sender_id = 1)
ORDER BY m.message_id DESC
) t2
GROUP BY t2.user_id
ORDER BY t2.message_id DESC
<强>结果:强>
+------------+------------+--------------+
| message_id | first_name | message_text |
+------------+------------+--------------+
| 10 | George | Howdy |
| 9 | Sarah | Hello dear |
| 5 | Peter | Good Day |
| 4 | John | Greetings |
答案 1 :(得分:2)
如果您GROUP BY
要删除重复的字段。在这种情况下,GROUP BY tbl_users.first_name
应该可以解决问题。
答案 2 :(得分:0)
我不确定你是否可以通过加入实现这一目标。子选择查询可以帮助您。
select a.first_name,
(select message_text from tbl_messages b
where (a.user_id = b.sender_id and b.sender_id = 1)
or (a.user_id = b.receiver_id and b.receiver_id = 1)
order by message_id DESC
LIMIT 1)
from tbl_users a;
答案 3 :(得分:0)
您在ORDER BY子句中缺少表名。另外我建议在表格上使用别名。因此,您的查询可能如下所示:
/* All Mike's messages */
SELECT
m.message_id,
u.first_name,
m.message_text
FROM
tbl_users AS u,
tbl_messages AS m
WHERE
(
u.user_id = m.sender_id AND
m.receiver_id = 1
) OR (
u.user_id = m.receiver_id AND
m.sender_id = 1
)
ORDER BY
m.message_id DESC;
但是,我认为最好分别获取已发送和已接收的消息结果。所以:
/* Messages sent my Mike */
SELECT
m.message_id,
u.first_name,
m.message_text
FROM
tbl_users AS u
INNER JOIN tbl_messages AS m ON m.receiver_id = u.user_id
WHERE
m.sender_id = 1
ORDER BY
m.message_id DESC;
/* Messages received my Mike */
SELECT
m.message_id,
u.first_name,
m.message_text
FROM
tbl_users AS u
INNER JOIN tbl_messages AS m ON u.user_id = m.sender_id
WHERE
m.receiver_id = 1
ORDER BY
m.message_id DESC;