I'm using MySQL for this.
(I'm stripping out irrelevant joins to keep this a bit clearer)
I'm trying to select companies' addresses, grouping by company_id, but exclude an entire user if they have any addresses in a specific country (with ID 242)
So the first query I tried was:
SELECT c.company_id
FROM companies c
NATURAL JOIN addresses a
WHERE a.country_id != 15
GROUP BY c.company_id
From what I understand, this does the select first, then excludes the rows which have a country_id of 15, then returns the company_id of the remaining rows, which are distinct, as they have been grouped.
So this returns any company who has at least one address outside of country 15. What I need is to exclude any company who has an address in that country.
How can I do this?