代码之家  ›  专栏  ›  技术社区  ›  Thiago Belem

获取某个类别中带有附件的帖子?

  •  2
  • Thiago Belem  · 技术社区  · 16 年前

    SELECT post.*
    FROM `bma_posts` AS post
    WHERE
        EXISTS (
            SELECT 1
            FROM `bma_posts` AS attachment
            JOIN `bma_term_relationships` AS relationship ON
                relationship.`object_id` = attachment.`ID`
                AND
                relationship.`term_taxonomy_id` IN (17,15,16,5)
            WHERE
                attachment.`post_parent` = post.`ID`
                AND
                attachment.`post_type` = 'attachment'
        )
        AND
        post.`post_type` = 'post'
        AND
        post.`post_status` = 'publish'
    ORDER BY post.`post_date` DESC
    LIMIT 3
    

    现在的问题是我拿不到附件。 ID 要知道“谁”在查询中包含了那个帖子。

    1 回复  |  直到 11 年前
        1
  •  1
  •   Thomas    16 年前
    Select ...
    From wp_posts As P
    Where Exists    (
                    Select 1
                    From wp_posts As P1
                        Join wp_term_relationship As WTR1
                            On WTR1.object_id = P1.ID
                                And WTR1.term_taxonomy_id In(3)
                    Where P1.post_parent = P.Id
                        And P1.post_type = 'attachment'
                    )
        And P.post_type = 'post'
    Order By p.post_date DESC
    Limit 15