Search code examples
mysqlwoocommercecoupon

Updating Woocommerce Coupon descriptions mysql


in my database this gives me the correct coupon results im looking for

SELECT p.`ID`, p.`post_title`, p.`post_excerpt`
FROM   `wp_posts` AS p
INNER JOIN `wp_postmeta` AS pm ON  p.`ID` = pm.`post_id`
WHERE  p.`post_type` = 'shop_coupon'
AND pm.`meta_key` = 'product_ids'
AND pm.`meta_value` = '131239'

but i need to update all these coupons its over 5k in coupons and the standard import/merge is not updating the coupon description, its essential that our coupons have the correct description due to our accounting system so i need to update them by sql

UPDATE p SET p.post_excerpt = 'emesa'
FROM wp_posts AS p
INNER JOIN wp_postmeta AS pm
ON p.ID = pm.post_id
WHERE p.post_type = 'shop_coupon'
AND pm.meta_key = 'product_ids'
AND pm.meta_value = '131239'

using update set does not work.


Solution

  • Update query syntax is different from the Select query. In Update query, you first specify all the tables (including joins) and then use Set clause. There is no usage of From in the Update query.

    From the MySQL Documentation:

    UPDATE [LOW_PRIORITY] [IGNORE] table_reference SET assignment_list [WHERE where_condition] [ORDER BY ...] [LIMIT row_count]

    value: {expr | DEFAULT}

    assignment: col_name = value

    assignment_list: assignment [, assignment] ...

    Following will be the correct query:

    UPDATE wp_posts AS p
    INNER JOIN wp_postmeta AS pm
    ON p.ID = pm.post_id 
    SET p.post_excerpt = 'emesa'
    WHERE p.post_type = 'shop_coupon'
    AND pm.meta_key = 'product_ids'
    AND pm.meta_value = '131239'