0
SELECT * FROM(
SELECT c_id, user_id, message, state, time FROM message 
WHERE receive=1 
ORDER BY message_id DESC
)AS t GROUP BY c_id

我有 mysql 语句可以获取用户的最后一条消息。

但是我需要加入用户表,所以我可以取出发件人的名字

LEFT JOIN user ON user.user_id=message.user_id

如何做到这一点?

//user
user_id name

//conversation
c_id    user_id

//message
m_id    c_id    user_id(sender) receive message state   time
4

1 回答 1

1
SELECT * FROM (
    SELECT c_id, user_id, message, state, time 
    FROM message 
    WHERE receive=1 
    ORDER BY message_id DESC) AS t 
LEFT JOIN user ON user.user_id=t.user_id 
GROUP BY c_id

PS 检查 user.user_id 是否存在。

于 2013-07-23T14:00:20.373 回答