SELECT DISTINCT pm.meta_value AS user_id, pm.post_id AS order_id ,ord.order_id as xmlorder_id,ord.id FROM wp0t_postmeta AS pm LEFT JOIN wp0t_posts AS p ON pm.post_id = p.ID LEFT JOIN wp0t_postmeta AS pm2 ON p.ID = pm2.post_id LEFT JOIN order_xml_gererate AS ord ON p.ID = ord.order_id WHERE ( p.post_type = 'shop_order' AND p.post_status NOT IN ('trash','wc-refunded') AND pm.meta_key = '_customer_user' AND pm2.meta_key IN ( '_paid_date', '_paid_date', '_date_completed', '_completed_date' ) AND pm2.meta_value != '' AND pm2.post_id > 27405 AND ord.order_id IS NULL ) ORDER BY pm.post_id DESC