Advertisement
businessdad

WooCommerce Currency Switcher - Cleanup duplicate data

Apr 16th, 2017
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.22 KB | None | 0 0
  1. DELETE FROM wp_postmeta
  2. WHERE meta_id IN
  3. (
  4.   SELECT meta_id
  5.   FROM
  6.   (
  7.     -- This query will return a list of all the meta rows that are duplicates and
  8.     -- that should be removed
  9.     SELECT
  10.       PM.meta_id
  11.       ,PM.meta_key
  12.       ,PM.post_id
  13.       ,meta_duplicates.meta_id_to_keep
  14.       ,meta_duplicates.duplicates_count
  15.     FROM
  16.       wp_postmeta PM
  17.       JOIN
  18.       (
  19.         SELECT
  20.           post_id
  21.           ,meta_key
  22.           ,min(meta_id) as meta_id_to_keep
  23.           ,count(meta_key) as duplicates_count
  24.         FROM
  25.           wp_postmeta
  26.         WHERE
  27.           (meta_key like '%_base_currency%')
  28.         GROUP BY
  29.           post_id,
  30.           meta_key
  31.         HAVING
  32.           duplicates_count > 1
  33.       ) AS meta_duplicates ON
  34.       (meta_duplicates.post_id = PM.post_id) AND
  35.       (meta_duplicates.meta_key = PM.meta_key)
  36.     -- This clause filters out the meta rows that we want to keep
  37.     WHERE
  38.       -- If you used MIN() above, you should check for "(PM.meta_id > meta_duplicates.meta_id_to_keep)"
  39.       -- If you used MAX() above, you should check for "(PM.meta_id < meta_duplicates.meta_id_to_keep)"
  40.       (PM.meta_id > meta_duplicates.meta_id_to_keep)
  41.   ) AS duplicates_to_remove
  42. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement