SQL Queries

Retrieving perk code assignments

select a.perk_id, a.subscription_id, a.code, o.customer_id, o.billing_email, m.meta_value as recipient_id, l.email as recipient_email 
from (
    select * 
    from wp_jc_allocated_subscription_perk_codes 
    where perk_id in ( 731543, 731549 ) 
    and subscription_id != 0
) as a 
left join (
    select * 
    from wp_wc_orders_meta 
    where meta_key = "_recipient_user"
) as m on a.subscription_id = m.order_id 
left join wp_wc_orders o on o.id = a.subscription_id 
left join wp_wc_customer_lookup l on m.meta_value = l.user_id;