大家好我有这个查询用于获取用户X与最后一条消息插入的所有对话,我的问题是它为每个对话选择了2个结果并且为了工作正常,每个对话的结果应该是1行。
这是我的查询,在这种情况下,我想要与id为1的用户进行对话
Inbox_msg表
inbox_join表
SELECT DISTINCT (
inbox_join.id_conversation
), user_chat.name AS name_conv, user_chat.surname AS surname_conv, user_chat.id, (
SELECT DISTINCT (
message
)
FROM inbox_msg
WHERE msg.id_conversation = inbox_msg.id_conversation
ORDER BY occured_at DESC
LIMIT 1
) AS last_msg, users.name, users.surname
FROM inbox_join
INNER JOIN inbox_msg AS msg ON msg.id_conversation = inbox_join.id_conversation
INNER JOIN users ON users.id = msg.id_user
INNER JOIN users AS user_chat ON user_chat.id <>1
AND (
inbox_join.id_user_2 = user_chat.id || inbox_join.id_user = user_chat.id
)
WHERE inbox_join.id_user =1
OR inbox_join.id_user_2 =1