I would recommend a pivot query. Using your example:
1 2 3 4 5 6 7 8 9 10 11 12 | SELECT p.ID, p.post_title, MAX(CASE WHEN wp_postmeta.meta_key = 'first_field' then wp_postmeta.meta_value ELSE NULL END) as first_field, MAX(CASE WHEN wp_postmeta.meta_key = 'second_field' then wp_postmeta.meta_value ELSE NULL END) as second_field, MAX(CASE WHEN wp_postmeta.meta_key = 'third_field' then wp_postmeta.meta_value ELSE NULL END) as third_field, FROM wp_posts p LEFT JOIN wp_postmeta pm1 ON ( pm1.post_id = p.ID) GROUP BY wp_posts.ID,wp_posts.post_title |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.