0

So I have query with the following where statement. Basically, I want to ensure there was no sports or player activity within the last 7 days.

WHERE......   
AND (( (SELECT max(sportswagerbyleague.asOfDate) 
            FROM sportswagerbyleague 
            WHERE sportswagerbyleague.userID = customer_profile.userID)  
                <= date(CURDATE()) - INTERVAL 7 DAY  ) 
                AND ((SELECT max(pa.txndate) 
                    FROM player_activity pa 
                    WHERE pa.userID = customer_profile.userID) <= date(CURDATE()) - INTERVAL 7 DAY ) )

However, if there is a NULL value (aka that user either has no player activity or sports activity), then the WHERE statement becomes false for that particular user. I want the WHERE statement to remain true even for a single NULL value.

Suggestions?

2 Answers 2

1

Comparing a value against a NULL value is always false, so you have to handle that case separately. You could use something like IS NULL for that purpose.

Sign up to request clarification or add additional context in comments.

Comments

0

Switch to NOT EXISTS instead to avoid three-valued-logic:

WHERE......   
AND NOT EXISTS
 ( SELECT *
   FROM sportswagerbyleague 
   WHERE sportswagerbyleague.userID = customer_profile.userID  
     AND sportswagerbyleague.asOfDate > date(CURDATE()) - INTERVAL 7 DAY
 ) 
AND NOT EXISTS
 (
   SELECT *max(pa.txndate) 
   FROM player_activity pa 
   WHERE pa.userID = customer_profile.userID
     AND pa.txndate > date(CURDATE()) - INTERVAL 7 DAY
 )

2 Comments

any reason why it is " * " instead of "max(sportswagerbyleague.asOfDate) " or is it just a typo?
No type, aggregation like max(sportswagerbyleague.asOfDate) will always return a row (NULL if there's no matching row) , so NOT EXISTS will never be true. [NOT] EXISTS is the only place where * is actually recommended :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.