DELETE from wp_postmeta WHERE meta_id IN ( SELECT meta_id_to_remove FROM ( SELECT PM.post_id ,PM.meta_key /* - Use MIN() if the original meta was added a second time, and it has a higher ID * than the duplicate. * - Use MAX() if the original meta was migrated (i.e. post ID changed), * and it has a lower ID than the duplicate. */ ,min(PM.meta_id) as meta_id_to_remove ,count(PM.meta_key) as meta_count FROM wp_posts P JOIN wp_postmeta PM ON (PM.post_id = P.ID) AND (PM.meta_key like '%_base_currency') WHERE (P.post_type = 'shop_order') GROUP BY PM.post_id ,PM.meta_key HAVING meta_count > 1 ) AS duplicates )