选择说明以获取最新的帖子版本

时间:2019-03-19 21:02:06

标签: mysql greatest-n-per-group

我有两个表,它们是:

posts (which contains the core post data):
    id
    type
    is_published
    visibility
    enable_comments
    views

post_versions (which contains the posts version such as drafts and final version):
    id
    post (foreign key posts.id)
    format
    title
    slug
    description
    thumbnail
    content
    created_by

我想做的是通过检索每个帖子的最新版本来获取所有帖子。我知道如何获取所有帖子的所有版本,例如:

    SELECT p.id, p.is_published, p.visibility, p.enable_comments, p.views,
           pv.format, pv.title, pv.slug, pv.description, pv.thumbnail,
           pv.content, pv.created_by
      FROM posts p
INNER JOIN post_versions pv on (p.id = pv.post)
     WHERE p.type = 1

但是我不知道如何对每条posts记录联接和获得一行post_versions中的最新记录。

0 个答案:

没有答案