I need help putting together an SQL that can to remove users if they don’t have (metakey1 or metakey2) and (does not have comments)
I have this SQL which does it for single meta_key
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
How can i extend the above SQL to do that?
2
Answers
You can use
in
in theon
clause:You get no matches only if all the metakeys are missing, which I think is what you are asking for.
EDIT:
You seem to want:
I prefer
NOT EXISTS
if you are going to have multiple comparisons to different tables.Try doing a delete with an exists clause asserting the requirement of either of two keys:
Note that the following where clause is no longer needed in the version of the query I wrote above:
It is no longer needed because
NOT EXISTS
now handles the job which the exclusion join was handling previously.