Select distinct column and latest on created_at and attach has many [duplicate]

How can I:

  • list the latest rooms_message on room
  • with discussions_messages.text (from discussions_messages)
  • and discussions_replies.replies (belongsTo discussions_messages) (from discussions_replies)?

I read many post (SQL select only rows with max value on a column or /a/38505618) but it’s not the same problem : I can get the latest but I d’ont have the good matching id on latest. I explain it :

I have 3 tables :

rooms_messages

id room_uuid discussion_message_uuid created_at
1 10 101 2024-07-16 12:30:45
2 20 102 2024-07-16 12:30:50
3 10 103 2024-07-16 12:32:45
4 20 104 2024-07-16 12:34:50
5 20 105 2024-07-16 12:36:50

discussions_messages

id text
101 Hello
102 Test
103 Ok
104 Wow
105 Hello2

discussions_replies

id discussion_message_uuid text
201 101 Hello
202 101 Test
203 102 Ok
204 102 Wow

I use this request from /a/38505618, and it works to list the room_messages by created_at order by. But I don’t have the correct rooms_messages.uuid:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>select URL, max(DateVisited)
from <table>
group by URL
</code>
<code>select URL, max(DateVisited) from <table> group by URL </code>
select URL, max(DateVisited)
from <table>
group by URL

My query:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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, count(dm.uuid) as replies,
FROM rooms_messages AS rm
INNER JOIN discussions_messages AS dm ON rm.discussion_message_uuid = dm.uuid
LEFT OUTER JOIN discussions_replies AS dr ON dm.uuid = dr.discussion_message_uuid
GROUP BY room_uuid
</code>
<code>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, count(dm.uuid) as replies, FROM rooms_messages AS rm INNER JOIN discussions_messages AS dm ON rm.discussion_message_uuid = dm.uuid LEFT OUTER JOIN discussions_replies AS dr ON dm.uuid = dr.discussion_message_uuid GROUP BY room_uuid </code>
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, count(dm.uuid) as replies,
FROM rooms_messages AS rm
INNER JOIN discussions_messages AS dm ON rm.discussion_message_uuid  = dm.uuid
LEFT OUTER JOIN discussions_replies AS dr ON dm.uuid = dr.discussion_message_uuid
GROUP BY room_uuid

But I have 3 problems :

  • The rm.uuid isn’t correct : it’s not the latest
  • Consequence: the dm.text isn’t the latest because the rm.uuid isn’t the last
  • Consequence: I don’t load the replies

1

Your current query is invalid, and would not even run on MySQL with the ONLY_FULL_GROUP_BY flag enabled (which it should be). We can use ROW_NUMBER() here for a proper solution:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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;
</code>
<code>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; </code>
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;

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật