Does anyone have any ideas on how to solve this query?
There is a table with customers and a table with licenses. Each customer can have several licenses expiring at different times, some already expired and some not yet. If I want to select all the customers with at least one valid license, I do:
SELECT * FROM License
LEFT JOIN Customer USING (customer_id)
GROUP BY Customer.customer_id
HAVING License.expiry > NOW()
But how do I get the customers who have only expired licenses? If I do “HAVING License.expiry < NOW()” it’ll find the customers with expired licenses alright, but it will not exclude the customers who ALSO have valid licenses at the same time. I need to select the customers who have ONLY expired licenses.
“NOT HAVING” would solve the problem, but there’s no such thing…
Any help will be appreciated.
Try this query –