我正在尝试将每个在其行上的键 = 值对分组到按 ID 分组的一行中。
表 1. wp_posts
键:ID - post_title
表 2. wp_postmeta
键:post_id、wpcf-owner、wpcf-tel、wpcf-mob、wpcf-email...
使用这个查询,我得到了所有信息,但是对于每个键,我得到单独的行。我需要的是一个数组,其中所有内容都在一行中,按 wp_posts.ID 分组
SELECT wp_posts.ID, wp_posts.post_title, wp_postmeta.post_id, wp_postmeta.meta_key, wp_postmeta.meta_value
FROM wp_posts
JOIN wp_postmeta
ON wp_posts.ID = wp_postmeta.post_id
WHERE wp_posts.post_type = 'accommodation' AND wp_postmeta.meta_key LIKE 'wpcf%'
ORDER BY hic13_posts.post_title
结果是:
ID | post_title | key value
------------------------------
1 | TITLE | wpcf-owner
1 | TITLE | wpcf-tel
1 | TITLE | wpcf-mob
1 | TITLE | wpcf-email
2 | TITLE | wpcf-owner
2 | TITLE | wpcf-tel
2 | TITLE | wpcf-mob
2 | TITLE | wpcf-email
...
我需要的是:
ID | post_title | key value | key value | key value | key value
-----------------------------------------------------------------
1 | TITLE | wpcf-owner | wpcf-tel | wpcf-mob | wpcf-email
2 | TITLE | wpcf-owner | wpcf-tel | wpcf-mob | wpcf-email
如果我将命令添加到按 ID 分组,那么我只能从第二个表中获得一个键......
SELECT wp_posts.ID, wp_posts.post_title, wp_postmeta.post_id, wp_postmeta.meta_key, wp_postmeta.meta_value
FROM wp_posts
JOIN wp_postmeta
ON wp_posts.ID = wp_postmeta.post_id
WHERE wp_posts.post_type = 'accommodation' AND wp_postmeta.meta_key LIKE 'wpcf%'
GROUP BY hic13_posts.ID
ORDER BY hic13_posts.post_title
我只得到这个
ID | post_title | key value
------------------------------
1 | TITLE | wpcf-owner
2 | TITLE | wpcf-tel
希望这一切都有意义:-)
谢谢!