我有这个问题:
SELECT
wp_posts.post_author,
wp_posts.post_date,
wp_posts.post_title,
wp_posts.post_content,
wp_posts.post_excerpt,
wp_posts.guid,
wp_posts.post_name AS slug,
wp_posts.ID AS wp_post_id,
wp_posts.post_status,
wp_sfmembers.display_name,
wp_postmeta.meta_key,
wp_postmeta.meta_value
FROM
wp_posts
INNER JOIN wp_sfmembers ON wp_posts.post_author = wp_sfmembers.user_id
INNER JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
where post_status = 'publish' AND post_type = 'post'
ORDER BY post_date DESC
我只想将wp_postmeta合并到一个记录中。我希望这是有道理的。
提前谢谢。
答案 0 :(得分:1)
尝试以下方法。它假设您只想要我在where子句中列出的特定meta_key值。这适合您的示例,因为您排除了除该列表中的内容之外的所有meta_key值。它还假设您的示例中的meta_key值与每次需要作为列生成的相同。
select wp_posts.post_author,
wp_posts.post_date,
wp_posts.post_title,
wp_posts.post_content,
wp_posts.post_excerpt,
wp_posts.guid,
wp_posts.post_name as slug,
wp_posts.id as wp_post_id,
wp_posts.post_status,
wp_sfmembers.display_name,
min(case when meta_key = '_edit_lock' then wp_postmeta.meta_key end) as meta_key,
min(case when meta_key = '_edit_lock' then wp_postmeta.meta_value end) as meta_value,
min(case when meta_key = '_yoast_wpseo_focuskw' then wp_postmeta.meta_value end) as meta_focus,
min(case when meta_key = '_yoast_wpseo_title' then wp_postmeta.meta_value end) as meta_title,
min(case when meta_key = '_yoast_wpseo_metadesc' then wp_postmeta.meta_value end) as meta_desc
from wp_posts
join wp_sfmembers
on wp_posts.post_author = wp_sfmembers.user_id
join wp_postmeta
on wp_posts.id = wp_postmeta.post_id
where post_status = 'publish'
and post_type = 'post'
and meta_key in ('_edit_lock','_yoast_wpseo_focuskw','_yoast_wpseo_title','_yoast_wpseo_metadesc')
group by wp_posts.post_author,
wp_posts.post_date,
wp_posts.post_title,
wp_posts.post_content,
wp_posts.post_excerpt,
wp_posts.guid,
wp_posts.post_name,
wp_posts.id,
wp_posts.post_status,
wp_sfmembers.display_name
order by post_date desc