I have a table called "countries" related to networks of other tables with many, many relationships:
countries countries_networks networks
+-------------+----------+ +-------------+----------+ +-------------+---------------+
| Field | Type | | Field | Type | | Field | Type |
+-------------+----------+ +-------------+----------+ +-------------+---------------+
| id | int(11) | | id | int(11) | | id | int(11) |
| countryName | char(35) | | country_id | int(11) | | name | varchar(100) |
+-------------+----------+ | network_id | int(11) | | description | varchar(255) |
To get all countries that have network_id of 6 and 7, I just do the following: (I could go further to use network.name, but I know country_networks.network_id, so I just use them to shorten SQL.)
SELECT DISTINCT countryName
FROM countries AS Country
INNER JOIN countries_networks AS n ON Country.id = n.country_id
WHERE n.network_id IN (6,7)
This is normal, but then I want to get countries with network_id from JUST 8 and others.
I tried the following, but its still returning networks with 6 and 7 inches. Is this something related to my JOIN?
SELECT DISTINCT countryName
FROM countries AS Country
INNER JOIN countries_networks AS n ON Country.id = n.country_id
WHERE n.network_id IN (8)
AND n.network_id not IN(6,7)
Thank.
source
share