/* Delete all meta values '_thumnbail_id' for posts not IN category */
DELETE FROM wp_postmeta WHERE post_id IN (
	SELECT ID 
	FROM wp_posts p
	JOIN wp_term_relationships tr ON (p.ID = tr.object_id)
	JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
	JOIN wp_terms t ON (tt.term_id = t.term_id)
	WHERE p.post_type='post'
	AND p.post_status = 'publish'
	AND p.id NOT IN (...)
	AND tt.taxonomy = 'category'
	AND t.term_id NOT IN (...)
) AND meta_key = '_thumbnail_id';



DELETE FROM wp_postmeta WHERE meta_key = '_thumbnail_id' AND meta_value NOT IN (
	SELECT ID
	FROM wp_posts p
	WHERE p.post_type = 'attachment'
)