我想知道如何在 SQL 中获得 LEFT JOIN 的计数。
目前我有3张桌子:
rooms_messages
id | 房间_id | 讨论_消息_id | 创建于 |
---|---|---|---|
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
(有很多discussions_replies
)
id | 文字 |
---|---|
101 | 你好 |
102 | 测试 |
103 | 好的 |
104 | 哇 |
105 | 你好2 |
discussions_replies
id | 讨论_消息_id | 文字 |
---|---|---|
201 | 103 | 你好 |
202 | 103 | 测试 |
我使用此查询来显示最新的 rooms_messages 并附加 discussions_messages 文本。但回复不正确:
WITH cte AS (
SELECT rm.id as id, rm.created_at, rm.room_id,
rm.discussion_message_id, dm.text, count(dm.id) as replies,
ROW_NUMBER() OVER (PARTITION BY rm.room_id ORDER BY rm.created_at DESC) rn
FROM rooms_messages AS rm
INNER JOIN discussions_messages AS dm ON rm.discussion_message_id = dm.id
LEFT JOIN discussions_replies AS dr ON dm.id = dr.discussion_message_id
GROUP BY dm.id
)
SELECT id, created_at, room_id, discussion_message_id, text, replies
FROM cte
WHERE rn = 1
我想显示附加在 discussions_messages 上的 discussions_replies。
我觉得问题出在线上:
count(dm.id) as replies,
...
INNER JOIN discussions_messages AS dm ON rm.discussion_message_id = dm.id
LEFT JOIN discussions_replies AS dr ON dm.id = dr.discussion_message_id
使用第二个 CTE 来获取计数,而不是在获取最后一行的同一 CTE 中执行此操作。
WITH cte AS (
SELECT rm.id as id, rm.created_at, rm.room_id,
rm.discussion_message_id, dm.text,
ROW_NUMBER() OVER (PARTITION BY rm.room_id ORDER BY rm.created_at DESC) rn
FROM rooms_messages AS rm
INNER JOIN discussions_messages AS dm ON rm.discussion_message_id = dm.id
LEFT JOIN discussions_replies AS dr ON dm.id = dr.discussion_message_id
), counts AS (
SELECT room_id, IFNULL(COUNT(dr.id), 0) AS replies
FROM rooms_messages AS rm
INNER JOIN discussions_messages AS dm ON rm.discussion_message_id = dm.id
LEFT JOIN discussions_replies AS dr ON dm.id = dr.discussion_message_id
GROUP BY room_id
)
SELECT id, created_at, cte.room_id, discussion_message_id, text, replies
FROM cte
JOIN counts ON cte.room_id = counts.room_id
WHERE rn = 1