0

I am attempting to run an SQL query on the post meta twice to update 2 values, I get the error:

Table 'wp_postmeta' is specified twice, both as a target for 'UPDATE' and as a separate source for data

I have attempted to combine the queries into 1, surely you can run 2 queries to the same table?

<?php
$wpdb->query(
    $wpdb->prepare(
        "UPDATE {$wpdb->prefix}postmeta
            SET meta_value = %s
            WHERE meta_key = %s
            AND meta_value = %s
            IN (
                SELECT post_id
                FROM {$wpdb->prefix}postmeta
                WHERE meta_value = %s
            )",
        $var1,
        '_regular_price',
        $var2
    )
);

$wpdb->query(
    $wpdb->prepare(
        "UPDATE {$wpdb->prefix}postmeta
            SET meta_value = %s
            WHERE meta_key = %s
            AND meta_value = %s
            IN (
                SELECT post_id
                FROM {$wpdb->prefix}postmeta
                WHERE meta_value = %s
            )",
        $var3,
        '_other_price',
        $var4
    )
);
2
  • I've googled that this is a common MySQL problem. People suggests to use join.
    – Max Yudin
    Commented Jan 27, 2018 at 19:24
  • 1
    not sure what is the wordpress aspect in this question. This is a mysql error which tells you your request is bad. If you need an explanation why, stackoverflow is probably a better place. Commented Jan 27, 2018 at 20:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.