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,753
  • 72
  • 57
  • 129
keruilin
  • 16,782
  • 34
  • 108
  • 175

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
  • 201,996
  • 37
  • 293
  • 362
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
  • 71,580
  • 16
  • 111
  • 150