SELECT DISTINCT message_group.group_id, messages.*
FROM messages
LEFT JOIN message_group ON message_group.group_id = messages.group_id
WHERE message_group.username = 'admin'
OR message_group.recipients = 'admin'
ORDER BY messages.id DESC
即使我指定了DISTINCT message_group.group_id
更多参考:https://stackoverflow.com/questions/36167801/how-to-order-message-groups-messages-just-like-imessage
答案 0 :(得分:0)
试试这个:
SELECT message_group.group_id, messages.*
FROM messages INNER JOIN
(SELECT
MAX(`timestamp`) AS latest,
group_id
FROM messages
GROUP BY group_id) m2
ON messages.group_id = m2.group_id AND messages.`timestamp` = m2.`timestamp`
LEFT JOIN message_group ON message_group.group_id = messages.group_id
WHERE message_group.username = 'admin'
OR message_group.recipients = 'admin'
ORDER BY messages.id DESC