Essentially, I’m trying to repair a broken WooCommerce database for a client.
I have a table wp_woocommerce_order_itemmeta and all of the _product_id values are erased. I am able to get the correct ID using this select statement. It will return all of the IDs, one per row as wanted.
SELECT
wp_wc_order_product_lookup.product_id
FROM
wp_woocommerce_order_items
INNER JOIN
wp_woocommerce_order_itemmeta
ON
wp_woocommerce_order_itemmeta.order_item_id = wp_woocommerce_order_items.order_item_id
INNER JOIN
wp_wc_order_product_lookup
ON
wp_wc_order_product_lookup.order_id = wp_woocommerce_order_items.order_id
WHERE
wp_woocommerce_order_itemmeta.meta_key = '_product_id'
Now when I tried to write an update statement I can’t get it to work. I’ve tried it several different ways. I read somewhere that SET
has to be moved past the INNER JOIN
lines.
UPDATE
wp_woocommerce_order_itemmeta
INNER JOIN
wp_woocommerce_order_itemmeta
ON
wp_woocommerce_order_itemmeta.order_item_id = wp_woocommerce_order_items.order_item_id
INNER JOIN
wp_wc_order_product_lookup
ON
wp_wc_order_product_lookup.order_id = wp_woocommerce_order_items.order_id
SET
wp_woocommerce_order_itemmeta.meta_value = wp_wc_order_product_lookup.order_id
WHERE
wp_woocommerce_order_itemmeta.meta_key = '_product_id'
That code produces the following error [Err] 1066 - Not unique table/alias: 'wp_woocommerce_order_itemmeta'
wp_woocommerce_order_itemmeta
wp_wc_order_product_lookup
2
Answers
Perhaps you’re after something like this:
you can solve this issue with simple PHP script, here is logic
1) select unique product_id and order_id from wp_wc_order_product_lookup
2) Result set from step 1 is used to update the table wp_woocommerce_order_itemmeta with the help of looping
Let me know if face any issue to write script in PHP then I will provide to code.