I have five tables:
models: id, name, specification models_networks: id, model_id, network_id networks: id, name, description countries_networks: id, country_id, network_id countries: id, countryName, etc, etc
models table is connected to the networks table via models_networks with a many to many relation.networks table is connected to the countries table via countries_networks with a many to many relationI need to do the following query, but I'm stuck:
Select all the models that will work in a specific country.
e.g.: say France has two networks. PigNetwork and CowNetwork. I want to get all the models that work on PigNetwork or CowNetwork, basically any that work in that country one way or the other.
If I've made myself clear, can someone help with the JOIN query please? I've only ever gone as far as joining two tables before. Thanks.
SELECT
m.name AS model_name,
c.countryName,
COUNT(*) AS network_count
FROM
models AS m
INNER JOIN models_networks AS mn ON mn.model_id = m.id
INNER JOIN networks AS n ON n.id = mn.network_id
INNER JOIN countries_networks AS cn ON cn.network_id = n.id
INNER JOIN countries AS c ON c.id = cn.country_id
WHERE
c.countryName = 'France'
GROUP BY
m.name,
c.countryName
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With