我想加入像这样的wp_posts和wp_postmeta表:
SELECT wp_posts.id, wp_posts.post_title, wp_posts.post_author, wp_postmeta.meta_key, wp_postmeta.meta_value
FROM wp_posts
LEFT JOIN wp_postmeta ON wp_posts.id = wp_postmeta.post_id
WHERE post_type = 'job';
但是我需要将wp_postmeta表中的键值对的值放在一行中;这样select语句看起来像这样:
SELECT wp_posts.id, wp_posts.post_title, wp_posts.post_author, wp_postmeta.meta_key1, wp_postmeta.meta_key2, wp_postmeta.meta_key3, wp_postmeta.meta_key4
这是wp_postmeta表中3行的显示方式:
** post_id meta_key meta_value ** 23841 job_start 1488386107 23841 job_requirements为空 23841 job_employment全职
如何通过select语句修改meta_values,将每个post_id的select语句结果放在一行?任何帮助表示赞赏。
答案 0 :(得分:0)
如果在wp_postmeta的字段中添加星号(),例如wp_postmeta。 这意味着您将获得所有列。
SELECT wp_posts.id, wp_posts.post_title, wp_posts.post_author, wp_postmeta.*
FROM wp_posts
LEFT JOIN wp_postmeta ON wp_posts.id = wp_postmeta.post_id
WHERE post_type = 'job';
试试吧。
答案 1 :(得分:0)
这是我正在寻找的解决方案。基于post_id,这允许我将wp_postmeta的meta_values放在一行中,其列值为wp_posts:
SELECT wp_posts.ID, wp_posts.post_title, wp_posts.post_author, pm1.meta_value as job_start_date, pm2.meta_value as job_end_date
FROM wp_posts
LEFT JOIN wp_postmeta AS pm1 ON (wp_posts.ID = pm1.post_id AND pm1.meta_key='job_start')
LEFT JOIN wp_postmeta AS pm2 ON (wp_posts.ID = pm2.post_id AND pm2.meta_key='job_end')
WHERE wp_posts.post_type = 'job';
我在这里找到了这个解决方案:http://subharanjan.com/sql-query-to-get-post-and-its-meta_values-as-one-row-of-record-single-data-grid/
如果我没有清楚地解决我的问题,我很抱歉。