我有10000个类别的帖子和1200000个帖子(每个帖子都有发布dae)。我想使用一个或两个SQL查询获取每个类别的最新帖子的日期。这是数据库的结构:
Categories +--+----+ |id|name| +--+----+ CategoriesToPosts +--------+----+ |category|post| +--------+----+ Posts +--+------------+- -+ |id|lastModified| ... | +--+------------+- -+
答案 0 :(得分:2)
使用JOIN,GROUP BY和MAX:
SELECT
CategoriesToPosts.category,
MAX(Posts.lastModified) AS lastModified
FROM CategoriesToPosts
LEFT JOIN Posts
ON Posts.id = CategoriesToPosts.post
GROUP BY CategoriesToPosts.category