Categories
MySql wordpress

SQL to extract order report

SELECT pm.meta_value AS city,p.ID as order_id,p.post_excerpt as
customer_note,p.post_date as order_date,pm2.meta_value as
suburb,pm3.meta_value as customer_id,
pm4.meta_value as sender_email,
pm5.meta_value as sender_firstname,
pm6.meta_value as sender_lastname
FROM wp_posts p
LEFT JOIN wp_postmeta pm ON p.ID = pm.post_id
LEFT JOIN wp_postmeta pm2 ON p.ID = pm2.post_id
LEFT JOIN wp_postmeta pm3 ON p.ID = pm3.post_id
LEFT JOIN wp_postmeta pm4 ON p.ID = pm4.post_id
LEFT JOIN wp_postmeta pm5 ON p.ID = pm5.post_id
LEFT JOIN wp_postmeta pm6 ON p.ID = pm6.post_id
WHERE p.post_type='shop_order'
AND p.post_status='wc-completed'
AND pm.meta_key='_shipping_address_2' AND pm.meta_value='Durban'
AND pm2.meta_key='_shipping_city'
AND pm3.meta_key='_customer_user'
AND pm4.meta_key='_billing_email'
AND pm5.meta_key='_billing_first_name'
AND pm6.meta_key='_billing_last_name'
ORDER BY p.post_date DESC

This SQL string will extract multiple meta values from an order.

South Coast Hosting Services (Pty) Ltd