1

When I execute the following query:

DELETE FROM wp_postmeta 
    WHERE post_id 
        NOT IN (
            SELECT post_id 
            FROM wp_postmeta a 
            INNER JOIN wp_sl_posts b 
            ON a.post_id = b.id
            )

I get an error message:

You can't specify target table 'wp_postmeta' for update in FROM clause  

I sort of know why, but I really don't know the syntax required here to perform the operation. I've tried a few different alternatives based on answers here and elsewhere, but not luck.

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
keruilin
  • 15,808
  • 32
  • 106
  • 174

2 Answers2

2

try

DELETE wp_postmeta 
FROM wp_postmeta 
left outer JOIN wp_sl_posts b ON a.post_id = b.id
WHERE b.post_id is null
juergen d
  • 195,137
  • 36
  • 275
  • 343
1

This is basically the same, and should work:

DELETE FROM wp_postmeta
WHERE post_id NOT IN (SELECT id FROM wp_sl_posts)
bfavaretto
  • 70,503
  • 15
  • 107
  • 148