我使用此SQL查询为我获取所有没有照片的帖子,它向我显示2010年的帖子,但是使用SQL命令可以删除这些2010年的帖子

SELECT *
  FROM wp_posts
  LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
   AND wp_postmeta.meta_key = '_thumbnail_id'
 WHERE 1 = 1 AND wp_postmeta.post_id IS NULL
   AND wp_posts.post_type = 'post'
   AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')
 GROUP BY wp_posts.ID
 ORDER BY wp_posts.post_date

最佳答案

一种选择是使用EXISTS

DELETE FROM wp_posts
 WHERE EXISTS
    (
    SELECT 1
      FROM wp_posts wp1
      LEFT JOIN wp_postmeta wp2 ON wp1.ID = wp2.post_id AND wp2.meta_key = '_thumbnail_id'
     WHERE wp2.post_id IS NULL
       AND wp1.post_type = 'post'
       AND (wp1.post_status = 'publish' OR wp1.post_status = 'private')
       AND wp1.ID = wp_posts.ID
     GROUP BY wp1.ID
     )


INNER JOIN

DELETE w
  FROM wp_posts w JOIN
    (
    SELECT wp1.ID
      FROM wp_posts wp1
      LEFT JOIN wp_postmeta wp2 ON wp1.ID = wp2.post_id AND wp2.meta_key = '_thumbnail_id'
     WHERE wp2.post_id IS NULL
       AND wp1.post_type = 'post'
       AND (wp1.post_status = 'publish' OR wp1.post_status = 'private')
     GROUP BY wp1.ID
     ) w2
     ON w.ID = w2.ID

09-15 19:39