我想知道我该怎么做:
房间消息:
讨论_消息:
讨论_回复:
https://stackoverflow.com/a/38505618的请求,并且可以按create_at order by显示列表room_messages。但我没有正确的 rooms_messages.uuid:
select URL, max(DateVisited)
from <table>
group by URL
我的询问:
SELECT rm.uuid as uuid, max(rm.created_at) as created_at, rm.room_uuid as room_uuid, rm.discussion_message_uuid as discussion_message_uuid, dm.text,
FROM rooms_messages AS rm
INNER JOIN discussions_messages AS dm ON rm.discussion_message_uuid = dm.uuid
LEFT JOIN discussions_replies AS dr ON dm.uuid = dr.discussion_message_uuid
GROUP BY room_uuid
但是我有3个问题:
ONLY_FULL_GROUP_BY
标志的 MySQL 上运行(它应该是这样)。 我们可以在这里使用
ROW_NUMBER()
来获得正确的解决方案:
WITH cte AS (
SELECT rm.uuid as uuid, rm.created_at, rm.room_uuid,
rm.discussion_message_uuid, dm.text,
ROW_NUMBER() OVER (PARTITION BY rm.room_uuid ORDER BY rm.created_at DESC) rn
FROM rooms_messages AS rm
INNER JOIN discussions_messages AS dm ON rm.discussion_message_uuid = dm.uuid
LEFT JOIN discussions_replies AS dr ON dm.uuid = dr.discussion_message_uuid
)
SELECT uuid, created_at, room_uuid, discussion_message_uuid, text
FROM cte
WHERE rn = 1;