sql >> データベース >  >> RDS >> Mysql

カテゴリを持つすべてのWordPress投稿を抽出するSQLクエリ

    これが私にとってうまくいった最後の答えです。

    SELECT DISTINCT
    post_title
    , post_content
    ,(SELECT meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = 'Asking Price (US\$)' AND wp_postmeta.post_id = wp_posts.ID) AS "Asking Price (US\$)"
    ,(SELECT group_concat(wp_terms.name separator ', ') 
        FROM wp_terms
        INNER JOIN wp_term_taxonomy on wp_terms.term_id = wp_term_taxonomy.term_id
        INNER JOIN wp_term_relationships wpr on wpr.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
        WHERE taxonomy= 'category' and wp_posts.ID = wpr.object_id
    ) AS "Categories"
    ,(SELECT group_concat(wp_terms.name separator ', ') 
        FROM wp_terms
        INNER JOIN wp_term_taxonomy on wp_terms.term_id = wp_term_taxonomy.term_id
        INNER JOIN wp_term_relationships wpr on wpr.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
        WHERE taxonomy= 'post_tag' and wp_posts.ID = wpr.object_id
    ) AS "Tags"
    FROM wp_posts
    WHERE post_type = 'post' 
    ORDER BY
    post_title
    , post_content
    



    1. SQLデータ定義言語

    2. SQLDeveloperでのSQLステートメントの調整

    3. PostgreSQL作成スキーマ

    4. 数字からのMySQLMONTHNAME()