Ik weet niet zeker waarom je een subquery gebruikt in de select
. Je selecteert ook kolommen die je niet wilt/nodig hebt.
Doet dit wat je wilt?
select p.user_id,
max( CASE WHEN pm.meta_key = '_billing_first_name' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_first_name,
max( CASE WHEN pm.meta_key = '_billing_last_name' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_last_name,
max( CASE WHEN pm.meta_key = '_billing_address_1' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_address_1,
group_concat(distinct oi.order_item_name separator '|' ) as order_items
from wp_posts p join
wp_postmeta pm
on p.ID = pm.post_id join
wp_woocommerce_order_items oi
on p.ID = oi.order_id
where p.post_type = 'shop_order' and
p.post_status = 'wc-completed' and
oi.order_item_name = 'Product Name'
group by p.user_id;
Ik moest speculeren over waar sommige kolommen vandaan komen, omdat je vraag niet expliciet is.