用于删除具有多个元键和注释的用户的SQL查询

时间:2019-03-12 作者:Guru Surfer

我需要帮助来创建一个SQL,如果用户没有(metakey1或metakey2)和(没有注释),则可以删除用户

我有一个SQL,它可以为单个meta\\u键执行此操作

SELECT *
FROM wp_users LEFT JOIN wp_usermeta
ON wp_users.ID = wp_usermeta.user_id
AND wp_usermeta.meta_key = \'metakey1\'
WHERE wp_usermeta.user_id IS NULL
如何扩展上述SQL来实现这一点?

1 个回复
SO网友:Guru Surfer

所以我得到了答案和解决方案https://stackoverflow.com/questions/55142768/sql-for-wp-to-delete-users-with-multiple-meta-keys-and-comments/55142835#55142835

SELECT u.*
FROM wp_users u
WHERE NOT EXISTS (SELECT 1
              FROM wp_usermeta um
              WHERE u.ID = um.user_id AND
                    um.meta_key IN (\'metakey1\', \'metakey2\')
             ) AND
  NOT EXISTS (SELECT 1
              FROM wp_comments c
              WHERE u.ID = c.user_id
             );