I want to select all the characters that your character HAS NOT challenged in the past 24 hours.
SELECT * FROM challenges
WHERE userCharID = 642 AND chalTime > DATE_SUB(CURDATE(), INTERVAL 1 DAY)
This returns a few rows with challenges that your character has initiated in the past day
SELECT characterID FROM CHARACTERS
WHERE NOT EXISTS (SELECT * FROM challenges
WHERE userCharID = '610'
AND chalTime > DATE_SUB(CURDATE(), INTERVAL 1 DAY))
Am I using WHERE NOT EXISTS wrong?
Yes. You want to be using NOT IN rather than NOT EXISTS. If you use NOT EXISTS and the non-existential sub-query returns any rows, then the condition will be false and no data will be returned by the main query. If no rows are returned, then the condition will be true and all rows will be returned by the main query (since, in this example, there are no other criteria in the main query). Often, the sub-query in a NOT EXISTS is a correlated sub-query, so the sub-query has to be evaluated for each row. Here, you don’t have a correlated sub-query (which is good for performance). But your query means ‘return information about all characters unless there exists some character who’s been challenged in the last one day by the nominated user’.
(In this analysis, I’ve quietly changed the SQL so that
userCharIDis always compared with a string, and with the value'642'specifically.)So, to find all the people that you have not challenged, you need to select all the users except those in the list you have challenged, which translates to:
This should give you the (possibly rather large) list of characters who you’ve not challenged within the last 24 hours.