Select rows as columns for wordpress post meta

前端 未结 3 690
一生所求
一生所求 2021-02-09 13:59

WordPress\'s wp_postmeta table has all the additional fields for a post but they are in rows so it\'s easy to add more.

However, now I want to query for all

3条回答
  •  一整个雨季
    2021-02-09 14:23

    Try that:

    select post_title , 
     MAX(CASE WHEN `meta_key`='total_related' THEN meta_value END)as 'total_related',
     MAX(CASE WHEN `meta_key` = 'updated' THEN meta_value END) as 'updated' ,
     MAX(CASE WHEN `meta_key` = 'cricket' THEN meta_value END) as 'cricket' 
    FROM   wp_posts p 
    JOIN wp_postmeta m ON p.id = m.post_id 
    GROUP BY p.id
    

提交回复
热议问题