Skip to content

Instantly share code, notes, and snippets.

@yanknudtskov
Last active August 31, 2023 09:55
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save yanknudtskov/035dbb2bcfacf8afa76b63d4ca6735aa to your computer and use it in GitHub Desktop.
Save yanknudtskov/035dbb2bcfacf8afa76b63d4ca6735aa to your computer and use it in GitHub Desktop.
Select Duplicate SKUs from WooCommerce Database #woocommerce #mysql
# SELECT any post_status
SELECT meta_value,COUNT(meta_value),GROUP_CONCAT(DISTINCT post_id ORDER BY post_id SEPARATOR ',') post_id
FROM wp_postmeta
WHERE meta_key = '_sku'
AND meta_value != ''
GROUP BY meta_value HAVING COUNT(meta_value) > 1
# SELECT only from products that are already published or in draft
SELECT meta_value,COUNT(meta_value),GROUP_CONCAT(DISTINCT post_id ORDER BY post_id SEPARATOR ',') post_id
FROM wp_postmeta
WHERE meta_key = '_sku'
AND meta_value != ''
AND post_id IN (SELECT ID FROM wp_posts WHERE post_status IN('draft', 'publish'))
GROUP BY meta_value HAVING COUNT(meta_value) > 1;
@seldimi
Copy link

seldimi commented Jul 29, 2022

wp_postmeta might contain orphan postmeta values from deleted records in wp_posts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment