I'm trying to see if its possible to do a specific query in mysql without having to resort to application based filtering and multiple queries.
Basically, I have a customers table, and I have a certificates table for every person ( including those which are not my customers)
Each customer can have multiple certificates, and I have the expiration date on each certificate.
I want to be able to select those customers whose certificate is about to expire (in 2 weeks or less) AND don't have another certificate with a longer expiration date.
The first part is easy, I perform an inner join based on ssn between the two tables, with the where clause specifing the date termination date from now to now + 2 weeks like so:
select certs.ssn, certs.cert_num, certs.cert_start, certs.cert_finish
from certs
INNER JOIN customers ON certs.ssn = customers.ssn
where certs.cert_finish < Date_ADD(now(), INTERVAL 14 DAY)
and certs.cert_finish > now()
However, I can't find a way to exclude those customers which have other certificates with a longer expiry date.
Do you guys have any ideas? I'm thinking a subquery based on max(cert_finish) on only those ssns which the previous query returned.