I have two tables:
t1
____________
projectID
userID
projectExpiration
t2
____________
usrID
subscriptioID
subscriptionExpiration
I need to select porject ID from T1 where the following conditions are met:
t1.projectExpiration = older then 3 months
t2.subscriptioID = '5'
t2.expiration = older then 3 months
The user may have other subscriptions in t2. I need ONLY the results where they have a single subscription entry which has an ID of ‘5’
I need help putting it all together.
Here’s what I’ve got so far:
SELECT projectID
FROM t1
LEFT JOIN t2 ON (t1.userID = t2.userID)
WHERE t1.projectExpiration < (CURDATE() - INTERVAL 3 MONTH)
AND t2.subscriptionExpiration = 5
AND t2.subscriptionExpiration < (CURDATE() - INTERVAL 3 MONTH)
Feel like I’m stuck…
You want a
not existscheck to make sure5is the only row:I should also mention that I used an
inner joinin lieu of aleft joinhere. This is because you don’t want to grab rows where at2row doesn’t exist (you’re otherwise not accounting fornullsin yourwhereclause). So, it’s just needless overhead. Aninner joingets us down to the result set you want in a much clearer fashion.