<分区>
我想获取论坛
的主题
列表,按最新事件排序:
- 创建线程
- 创建帖子
期望的输出:
Thread name | Timestamp (Thread) | Timestamp (latest post)
-----------------------------------------------------------
Thread A | 1 | 10 <- old thread, newest post (newer than newest thread)
Thread C | 3 | - <- newest thread, no post
Thread B | 2 | 5
编辑/可能的解决方案:
SELECT
t.*,
IFNULL
(
(
SELECT p.timestamp
FROM posts p
WHERE p.thread_id = t.id
ORDER BY p.timestamp DESC LIMIT 1
),
t.timestamp
) AS sorting
FROM
threads t
WHERE t.forum_id = 1
ORDER BY sorting DESC
有人有性能建议吗?谢谢大家!