问题描述
从POSTS-> ID中,我想要前12个ID,其中post_type ='product' 通过该ID关联,我想获取所有POST Meta。
我正在使用此MySQL查询,但它不起作用:
SELECT posts.ID,postMeta.Meta_key,postMeta.Meta_value FROM `wp_u8gwgg_posts` as posts
INNER JOIN wp_u8gwgg_postMeta as postMeta on posts.ID = postMeta.post_id
WHERE
posts.`post_type` = 'product' AND
posts.post_status = 'publish'
ORDER BY posts.ID
这给了我多余的数据post_id。
Post ID | Meta Key | Meta Value
++++++++++++++++++++++++++++++++++++++
12 | _sku | 18945236
12 | _price | 1569.36
12 | _regular_price | 1496.20
我希望结果为:
Post ID | Meta Key | Meta Value
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12 | _sku,_price,_regular_price | 18945236,1569.36,1496.20
如果可能,请让我解决。
OR
如果有更好的解决方法,也欢迎您。
谢谢
解决方法
SELECT posts.ID,GROUP_CONCAT(postmeta.meta_key ORDER BY posts.ID) meta_keys,GROUP_CONCAT(postmeta.meta_value ORDER BY posts.ID) meta_values
FROM wp_u8gwgg_posts as posts
INNER JOIN wp_u8gwgg_postmeta as postmeta on posts.ID = postmeta.post_id
WHERE posts.post_type = 'product'
AND posts.post_status = 'publish'
GROUP BY posts.ID
ORDER BY posts.ID